# finding the last element in a referenced array

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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,3];

i can access the last element by

\$hash[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[\$#hash]

but it doesn't work this way...

anybody willing to help, explain the mystics or simple

## Re: finding the last element in a referenced array

..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,3];
>
> i can access the last element by
>
> \$hash[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[\$#hash]
>
> but it doesn't work this way...
>
> anybody willing to help, explain the mystics or simple

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

\$hash[-1]

--- Shawn

## Re: finding the last element in a referenced array

On Tue, 05 Oct 2004 08:37:38 -0700, .rhavin wrote:
> \$hash=[1,2,3];
>
> i can access the last element by
>
> \$hash[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];

--
"What we anticipate seldom occurs. What we least expected generally
happens." (Benjamin Disraeli)

## Re: finding the last element in a referenced array

..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,3];
>
> i can access the last element by
>
> \$hash[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[\$#hash]

Look, the array is dereferenced by

@{ \$hash }

so the last element index would be

\$#{ \$hash }

so you'd get at the last element with

\$hash[ \$#{ \$hash } ];

Of course,

\$hash[ -1 ]

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

\$r = \$hash;
\$last = \$r->[ -1 ];
or
\$last = \$r->[ \$#\$r ];

Rhesa

## Re: finding the last element in a referenced array

*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?

## Re: finding the last element in a referenced array

..rhavin wrote:

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

## Re: finding the last element in a referenced array

True.

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

## Re: finding the last element in a referenced array

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