finding the last element in a referenced array

R

.rhavin

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

Tore Aursand

$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];
 
S

Shawn Corey

..rhavin said:
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
 
R

Rhesa Rozendaal

..rhavin said:
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
 
R

.rhavin

Rhesa Rozendaal said:
$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?
 
J

Joe Smith

..rhavin said:
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
 
A

Anno Siegel

Joe Smith said:
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
 
H

Heinrich Mislik

Rhesa Rozendaal said:
$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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top