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. Advertisements

  2. .rhavin

    Tore Aursand Guest

    You want to approach the array "from behind";

    my $last = $hash{'%1'}{'%2'}[-1];
     
    Tore Aursand, Oct 5, 2004
    #2
    1. Advertisements

  3. .rhavin

    Shawn Corey Guest

    ^^^
    Do you mean 'because?'
    To access the last element in an array use -1.

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

    --- Shawn
     
    Shawn Corey, Oct 5, 2004
    #3
  4. luckily for you, perl can count backwards too:

    @a = (1,2,3);
    print $a[-1]; # prints 3
    print $a[-2]; # prints 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

    *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

    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

    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. 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, Oct 8, 2004
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.