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.
In reply to Re: split to get the dir name
by dragonchild
in thread split to get the dir name
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |