Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: split to get the dir name

by dragonchild (Archbishop)
on Sep 29, 2003 at 15:34 UTC ( [id://294993]=note: print w/replies, xml ) Need Help??


in reply to split to get the dir name

open(LS, 'ls -l|') or die "Cannot open 'ls -l' for reading\n"; while (<LS>) { chomp; #Added after some thought. my $filename = (split ' ', $_)[-1]; print "$filename\n"; } close LS;

There's a whole bunch of Perl idioms in that line. split on ' ' is magical. It does the same thing as /\s+/. Negative indices for arrays count from the end. So, index -1 is the last element. I'm also using an implicit list populated with the result of the split.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: split to get the dir name
by dvergin (Monsignor) on Sep 29, 2003 at 18:56 UTC
    Quothe dragonchild:
        my $filename = (split ' ', $_)[-1];

    But what if the filename contains a space?

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

      Then split will break. You'd have to go with substr (which would be platform-dependent) or change the ls pipe from 'ls -l' to 'ls -1'. *shrugs*

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Just curious, how is substr platform-dependent? And how is ls not platfrom-dependent?

        Nonsense. You never liked me anyway. You wouldn’t even come to my birthday party!
Re: Re: split to get the dir name
by Plankton (Vicar) on Sep 29, 2003 at 15:40 UTC
    why -l? He only wants the file names, right? And why are you assuming that AM is executing the "ls -l" in his script? Maybe he is reading in a old log file or something else.
    update
    -l as in "ls -l" not 1 as in 1 2 3. My point being if AM is executing "ls -l" and dealing with the output of "ls -l" it would be simplier to execute "ls" with no options. The output of that is simplier to deal with and AM could avoid using split all together.
    update again
    open(LS, 'ls|') or die "Cannot open 'ls' for reading\n"; while (<LS>) { chomp; #Added after some thought. print "$_\n"; } close LS;

    You can't do this to me! I went to college!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://294993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found