Perl - how to get the number of elements in a list (not a named array)

Posted by NXT on Stack Overflow See other posts from Stack Overflow or by NXT
Published on 2010-05-09T23:56:47Z Indexed on 2010/05/10 0:08 UTC
Read the original article Hit count: 244

Filed under:
|
|

Hi Everyone,

I'm trying to get a block of code down to one line. I need a way to get the number of items in a list. My code currently looks like this:

# Include the lib directory several levels up from this directory
my @ary = split('/', $Bin);
my @ary = @ary[0 .. $#ary-4];
my $res = join '/',@ary;
lib->import($res.'/lib');

That's great but I'd like to make that one line, something like this:

lib->import( join('/', ((split('/', $Bin)) [0 .. $#ary-4]))  );

But of course the syntax $#ary is meaningless in the above line.

Is there equivalent way to get the number of elements in an anonymous list?

Thanks!

PS: The reason for consolidating this is that it will be in the header of a bunch of perl scripts that are ancillary to the main application, and I want this little incantation to be more cut & paste proof.

© Stack Overflow or respective owner

Related posts about perl

Related posts about arrays