in reply to Get Element inArray

my $res=$arr[-2]

UPDATE: changed $ instead of @

Replies are listed 'Best First'.
Re^2: Get Element inArray
by Thilosophy (Curate) on May 25, 2005 at 02:53 UTC
    To explain that a little, negative array indices count from the end of the array: -1 is the last element, -2 the one before that, and so on.

    Scalar value @arr[-2] better written as $arr[-2]

    And it should be $arr[-2] (with a $ sigil) at least in current Perl (Perl 6 will change that).

Re^2: Get Element inArray
by K_M_McMahon (Hermit) on May 25, 2005 at 02:54 UTC
    gopalr said my $res=@arr[-2]

    I think you mean my $res=$arr[-2];
    If you have warnings turned on (which we all should!) it doesn't like the @arr[-2] method. When referring to a scalar element of an array you should specify that it is scalar with $

    -Kevin
    my $a='62696c6c77667269656e6440676d61696c2e636f6d'; while ($a=~m/(^.{2})/s) {print unpack('A',pack('H*',"$1"));$a=~s/^.{2}//s;}