David Blevins wrote:
: I'm wondering, why can I do this:
: 
:     local @fileList = reverse sort `ls $list*.list`;
:     local $current = $fileList[0];
:     local $previous = $fileList[1];
: 
:     chop $current;
:     chop $previous;
: 
: But, not this:
:     local @fileList = reverse sort `ls $list*.list`;
:     local $current = chop $fileList[0];
:     local $previous = chop $fileList[1];

Because you can do this:

chomp(local @fileList = reverse sort `ls $list*.list`);
local $current = $fileList[0];
local $previous = $fileList[1];

or better yet, this:

chomp(my @fileList = reverse sort `ls $list*.list`);
my $current = $fileList[0];
my $previous = $fileList[1];

n.b. chop() removes the last character; chomp() removes the last
character only if it's a record separator (defined in $/). Either one
will return the character chopped.

: As a side note, I think it's great that I can do this (although I find it
: difficult to read, for now):
: 
:     local ($current,$previous) = (reverse sort `ls $list*.list`)[0,1];
: 
:     chop $current;
:     chop $previous;
: 
: Of course, I know there has to be a better way to do this:
: reverse sort `ls $list*.list`

Well, I don't know is this would be "better" by your definition, but I
prefer to do it this way:

opendir DIR, $directory or die "Can't opendir $directory: $!";
my @fileList = reverse sort grep /$list.*\.list$/, readdir DIR;
closedir DIR;

Longer, but no backticks, no shells in the way, no glob limits (I've
been burned by those), better control over the filenames you grab. And
no chomp() needed.

: I just get tired of looking everything up in my Perl book.

Try perldoc -h.

-- tdk

Reply via email to