in reply to question regarding slices

If the list is named @array, you can find the last element with $#array.

Consider the following code:

my @array = qw/this that the other/; local $, = ", "; print @array[1..$#array], "\n"; __OUTPUT__ that, the, other

An unnamed true list (one that isn't held within a scalar in any way, nor referenced by an array-ref) can't be indexed in such a way as to construct a 0..end-of-list, but you do have a couple of other options.

my $last_item = (this, that, the, other)[-1];

Or the more useful...

my $list = [qw/this that the other/]; local $, = ", "; print @{$list}[0..$#{$list}];

This last alternative uses an anonymous list referred to by an array-ref scalar.


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: question regarding slices
by Grygonos (Chaplain) on Nov 06, 2003 at 18:36 UTC
    the list has no name..the list is returned by readdir... which you can put into an array like so my @files = readdir(PRINT). However I'm trying to expand my foo and learn to ninja-code it in such a way that doesnt require a variable declaration. Probably shoulda been more clear about that in the original question. Thank you for your information though. I normally use that or just simply the array in a scalar context which will return the size as well.