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 | |
by dragonchild (Archbishop) on Sep 29, 2003 at 19:02 UTC | |
by Plankton (Vicar) on Sep 29, 2003 at 19:27 UTC | |
by dragonchild (Archbishop) on Sep 29, 2003 at 19:30 UTC | |
by Plankton (Vicar) on Sep 29, 2003 at 19:39 UTC | |
| |
|
Re: Re: split to get the dir name
by Plankton (Vicar) on Sep 29, 2003 at 15:40 UTC |