finding the last element in a referenced array

Discussion in 'Perl Misc' started by .rhavin, Oct 5, 2004.

  1. .rhavin

    .rhavin Guest

    i searched the usenet for a while and i wasn't able to find something
    useful. perhaps i looked in the wrong places or i am to stupid -
    whatever... here's my problem:

    i want to access the last elemt of an array... sounds like $#array, i
    know, but in my case, the array is stored as a reference in a
    construction like this:

    $hash{'%1'}{'%2'}=[1,2,3];

    i can access the last element by

    $hash{'%1'}{'%2'}[2]

    cos i know it is the second, but i need a way to access the last
    element without knowing whether it is the first, or the zeroth or
    whatever.

    i tried

    $hash{'%1'}{'%2'}[$#hash{'%1'}{'%2'}]

    but it doesn't work this way...

    anybody willing to help, explain the mystics or simple
    answer-pointing?
    .rhavin, Oct 5, 2004
    #1
    1. Advertising

  2. .rhavin

    Tore Aursand Guest

    On Tue, 05 Oct 2004 08:37:38 -0700, .rhavin wrote:
    > $hash{'%1'}{'%2'}=[1,2,3];
    >
    > i can access the last element by
    >
    > $hash{'%1'}{'%2'}[2]
    >
    > cos i know it is the second, but i need a way to access the last
    > element without knowing whether it is the first, or the zeroth or
    > whatever.


    You want to approach the array "from behind";

    my $last = $hash{'%1'}{'%2'}[-1];


    --
    Tore Aursand <>
    "What we anticipate seldom occurs. What we least expected generally
    happens." (Benjamin Disraeli)
    Tore Aursand, Oct 5, 2004
    #2
    1. Advertising

  3. .rhavin

    Shawn Corey Guest

    ..rhavin wrote:
    > i searched the usenet for a while and i wasn't able to find something
    > useful. perhaps i looked in the wrong places or i am to stupid -
    > whatever... here's my problem:
    >
    > i want to access the last elemt of an array... sounds like $#array, i
    > know, but in my case, the array is stored as a reference in a
    > construction like this:
    >
    > $hash{'%1'}{'%2'}=[1,2,3];
    >
    > i can access the last element by
    >
    > $hash{'%1'}{'%2'}[2]
    >
    > cos i know it is the second, but i need a way to access the last

    ^^^
    Do you mean 'because?'

    > element without knowing whether it is the first, or the zeroth or
    > whatever.
    >
    > i tried
    >
    > $hash{'%1'}{'%2'}[$#hash{'%1'}{'%2'}]
    >
    > but it doesn't work this way...
    >
    > anybody willing to help, explain the mystics or simple
    > answer-pointing?


    To access the last element in an array use -1.

    $hash{'%1'}{'%2'}[-1]

    --- Shawn
    Shawn Corey, Oct 5, 2004
    #3
  4. ..rhavin wrote:
    > i want to access the last elemt of an array... sounds like $#array, i
    > know, but in my case, the array is stored as a reference in a
    > construction like this:
    >
    > $hash{'%1'}{'%2'}=[1,2,3];
    >
    > i can access the last element by
    >
    > $hash{'%1'}{'%2'}[2]
    >
    > cos i know it is the second, but i need a way to access the last
    > element without knowing whether it is the first, or the zeroth or
    > whatever.


    luckily for you, perl can count backwards too:

    @a = (1,2,3);
    print $a[-1]; # prints 3
    print $a[-2]; # prints 2

    > i tried
    >
    > $hash{'%1'}{'%2'}[$#hash{'%1'}{'%2'}]


    Look, the array is dereferenced by

    @{ $hash{'%1'}{'%2'} }

    so the last element index would be

    $#{ $hash{'%1'}{'%2'} }

    so you'd get at the last element with

    $hash{'%1'}{'%2'}[ $#{ $hash{'%1'}{'%2'} } ];

    Of course,

    $hash{'%1'}{'%2'}[ -1 ]

    is a bit more readable...

    You could also have assigned the reference to a new variable:

    $r = $hash{'%1'}{'%2'};
    $last = $r->[ -1 ];
    or
    $last = $r->[ $#$r ];


    Rhesa
    Rhesa Rozendaal, Oct 5, 2004
    #4
  5. .rhavin

    .rhavin Guest

    Rhesa Rozendaal <perl&> wrote in message news:<>...

    > $hash{'%1'}{'%2'}[-1]


    *g* thanx a lot to all ... i really never thought of this simple
    solution ... i think i once again outed myself as a c-styler, thinkin
    the minus-first element is the element preceding the zeroth in a
    reference to the middle of an array - i guess now that kind of
    construction is impossible in perl?
    .rhavin, Oct 6, 2004
    #5
  6. .rhavin

    Joe Smith Guest

    ..rhavin wrote:

    > i think i once again outed myself as a c-styler, thinkin
    > the minus-first element is the element preceding the zeroth in a
    > reference to the middle of an array - i guess now that kind of
    > construction is impossible in perl?


    Correct. Arrays are first class citizens in the Perl world; not
    merely pointers to memory. There's really no concept of a
    reference to the middle of an array: Perl gives you a single
    scalar, or an array slice (which has its own set of array
    bounds checking).
    -Joe
    Joe Smith, Oct 6, 2004
    #6
  7. .rhavin

    Anno Siegel Guest

    Joe Smith <> wrote in comp.lang.perl.misc:
    > .rhavin wrote:
    >
    > > i think i once again outed myself as a c-styler, thinkin
    > > the minus-first element is the element preceding the zeroth in a
    > > reference to the middle of an array - i guess now that kind of
    > > construction is impossible in perl?

    >
    > Correct. Arrays are first class citizens in the Perl world; not
    > merely pointers to memory.


    True.

    > There's really no concept of a
    > reference to the middle of an array: Perl gives you a single
    > scalar, or an array slice (which has its own set of array
    > bounds checking).


    Also true, but it sounds like the second statement were a consequence
    of the first one, which it isn't. Perl's strings are also first class
    data (not just arrays of characters), but the concept of a string
    that is really part of another string does exist, realized by
    substr(). Similarly, Perl *could* have "sub-arrays", though it
    doesn't. If we had lvalue functions for arrays, not only scalars,
    it should be simple to implement sub-arrays in a class.

    Anno
    Anno Siegel, Oct 7, 2004
    #7
  8. In article <>, says...
    >
    >
    >Rhesa Rozendaal <perl&> wrote in message news:<>...
    >
    >> $hash{'%1'}{'%2'}[-1]

    >
    >*g* thanx a lot to all ... i really never thought of this simple
    >solution ... i think i once again outed myself as a c-styler, thinkin
    >the minus-first element is the element preceding the zeroth in a
    >reference to the middle of an array - i guess now that kind of
    >construction is impossible in perl?


    I know it's deprecated, but I didn't expect this strange results:

    perl -e '$[ = -4;@x = (1,2,3,4,5,6,7,8,9);print "$x[-1]\n";print "$x[0]\n";print "$x[1]\n"'

    prints

    4
    5
    6

    as kind of expected. But

    perl -e '$[ = -4;@x = (1,2,3,4,5,6,7,8,9);print "$_ -> $x[$_]\n" for -4 .. 4'

    prints

    -4 -> 6
    -3 -> 7
    -2 -> 8
    -1 -> 9
    0 -> 1
    1 -> 6
    2 -> 7
    3 -> 8
    4 -> 9

    Any explanation?

    For completeness:

    This is perl, v5.8.2 built for cygwin-thread-multi-64int

    Cheers

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Oct 8, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    The referenced element is undefined

    Timo Nentwig, May 25, 2004, in forum: XML
    Replies:
    4
    Views:
    467
    Timo Nentwig
    May 25, 2004
  2. Replies:
    5
    Views:
    447
    Ben Bacarisse
    Dec 6, 2006
  3. Johannes Schaub (litb)

    Advancing past the last element of an array

    Johannes Schaub (litb), Dec 27, 2009, in forum: C Programming
    Replies:
    10
    Views:
    607
    Tim Rentsch
    Jan 13, 2010
  4. Josselin
    Replies:
    7
    Views:
    127
    Nicolas Despr├Ęs
    Feb 21, 2007
  5. Len Lawrence

    Last element skip in array mapping

    Len Lawrence, Jun 13, 2009, in forum: Ruby
    Replies:
    2
    Views:
    88
    Len Lawrence
    Jun 13, 2009
Loading...

Share This Page