in reply to Re: Re: where to look up "funny looking" perl notation (and an example)
in thread where to look up "funny looking" perl notation (and an example)

Almost always true...

If it wasn't for the fact that you can change the base-index of perl arrays to something other then 0... But I've never seen anyone use that feature yet. The global parameter for the feature is '$['.

So use $# to get the last defined element index so you can safely add (or loop), and use scalar @array to find out how many elements there are in the array...

  • Comment on Re: Re: Re: where to look up "funny looking" perl notation (and an example)

Replies are listed 'Best First'.
Re: Re: Re: Re: where to look up "funny looking" perl notation (and an example)
by Mr. Muskrat (Canon) on Jan 21, 2003 at 20:52 UTC
    Read perldoc perldata again!

    Version 5 of Perl changed the semantics of $[: files that don't set the value of $[ no longer need to worry about whether another file changed its value. (In other words, use of $[ is deprecated.) So in general you can assume that
    scalar(@whatever) == $#whatever + 1;

      Very true! But I still felt the history was relevant, as it sort of explains why the $# exists at all... And one could still use it, if one wanted to... (Notice the repetitive usage of the word 'one' in this context ;) )
Re4: where to look up "funny looking" perl notation (and an example)
by Hofmator (Curate) on Jan 22, 2003 at 13:22 UTC
    For getting the 'last defined index' in an array - if you are using it as a subscript - I prefer $array[-1] = $foo; over $array[$#array] = $foo;

    $#array is still useful if you are looping over an array by index, like this

    for my $i (0..$#array) { # do something with $array[$i] }

    -- Hofmator