strange hash syntax

T

Torch

I encountered a hash syntax I have never seen before, can somebody
help me understanding it.

$somehash{$somekey} = [ @somearray];

When I do print"$somehash{$somekey}" I get some strange number
ARRAY(0x20038114)as an output.
What does this mean????
I can;t find anything in my book and I do not nkow how to start
looking for it on the net...
Thx for any help!

Torch
 
A

Andreas Kahari

I encountered a hash syntax I have never seen before, can somebody
help me understanding it.

$somehash{$somekey} = [ @somearray];


This stores an array reference at the given location of the
hash. The %somehash now contains an array reference at the key
$somekey. You can index it like this:

# Third element of the stored array:
my $e = $somehash{$somekey}[2];

Or loop over it:

foreach my $e (@{ $somehash{$somekey} }) {
# Make use of array element $e here
}


Read the perldata manual.
 
B

Brian McCauley

$somehash{$somekey} = [ @somearray];
What does this mean????
I can;t find anything in my book

Others have addressed your immediate problem but if your book doesn't
explain [...] it sounds like it is probably a Perl4 book. There's a
lot of stuff in Perl5 that's not in Perl4 and there's a lot of stuff
that you had to do in Perl4 to work around its shortcommings that's
now considered very poor style in Perl5.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
D

Dave Oswald

Torch said:
I encountered a hash syntax I have never seen before, can somebody
help me understanding it.

$somehash{$somekey} = [ @somearray];

When I do print"$somehash{$somekey}" I get some strange number
ARRAY(0x20038114)as an output.
What does this mean????
I can;t find anything in my book and I do not nkow how to start
looking for it on the net...

You're looking at the creation of an anonymous array, whos referent will be
a hash element.

If your book doesn't discuss references and anonymous arrays, you'd better
get a different book, or read 'perlref' from the Perl POD.
 
T

Tad McClellan

Darek N said:
# Third element of the stored array:
my $e = $somehash{$somekey}[2]; ## need to derefernce !!

This is not quite right


Yes it is.

because of the dereferencing action:
my $e = $somehash{$somekey}->[2];


perldoc perlref

One more thing here. The arrow is optional _between_
brackets subscripts...
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top