You are not logged in.

#1 2009-09-23 17:23:24

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

[SOLVED] Perl: array as hash key

Hi all,

How would I do the following in Perl:

 print ("%hash{@array}");

or

 print ("%hash{$var}");

Basically, I need my %hash to accept a variable as a key.

Thanks

Last edited by SIGTERM (2009-09-23 19:42:19)

Offline

#2 2009-09-23 18:17:25

zyghom
Member
From: Poland/currently Africa
Registered: 2006-05-11
Posts: 432
Website

Re: [SOLVED] Perl: array as hash key

what do you want to get ? hash of arrays ?


Zygfryd Homonto

Offline

#3 2009-09-23 18:29:35

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: [SOLVED] Perl: array as hash key

Uhm...
Instead of saying:

 %hash{"some_sort_of_key_here"}

I want to get the key from a variable or preferably from an array:

%hash{@array}

For example,

@array = ("1")
%hash = ("1" => ABC);
print %hash{@array};
ABC #print now prints the value for key "1" in %hash.

Offline

#4 2009-09-23 19:22:39

g0ju
Member
From: Chemnitz, Germany
Registered: 2009-08-08
Posts: 23

Re: [SOLVED] Perl: array as hash key

Do you mean something like that?

@array = ("1", "2");
%hash = ("1" => "foo", "2" => "bar");

print $hash{$array[0]};

Offline

#5 2009-09-23 19:24:13

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: [SOLVED] Perl: array as hash key

g0ju wrote:

Do you mean something like that?

@array = ("1", "2");
%hash = ("1" => "foo", "2" => "bar");

print $hash{$array[0]};

Yes, exactly! Is that possible?

Offline

#6 2009-09-23 19:37:41

g0ju
Member
From: Chemnitz, Germany
Registered: 2009-08-08
Posts: 23

Re: [SOLVED] Perl: array as hash key

What else do you want to do? Isn't that what you wanted?

Offline

#7 2009-09-23 19:42:05

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: [SOLVED] Perl: array as hash key

Oh, yeah. But I thought that was impossible, because I'd tried that already and it didn't work out. Must have addressed the hash wrongly.
Thanks a lot smile!

Offline

#8 2009-09-23 19:52:17

g0ju
Member
From: Chemnitz, Germany
Registered: 2009-08-08
Posts: 23

Re: [SOLVED] Perl: array as hash key

It's important that you understand the system of types in perl because it's a basic concept, you need to know if you want to program with perl.
Most perl tutorials (should) cover this topic very well.

Offline

#9 2009-09-23 23:39:02

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] Perl: array as hash key

@SIGTERM: Based on what you posted, I would guess that you tried %hash{@array[0]} instead of $hash{$array[0]}.  A common mistake, but you should definitely make sure you understand *why* it is a mistake before trying to do more complicated things.  The sigils (or, to use the proper term, "funny characters") in front of a name are secondary to determining its type; the array subscripting operator [] and the hash subscripting operator {} that come after the name "bind more tightly" (in CS terms).  When we write

$x{$y[$z]}

Perl knows that x is a hash (%x) because it's being subscripted with {}; y is an array (@y) because it's being subscripted with []; and z is a scalar ($z) because it's not being subscripted at all.  The fact that you use $ tells Perl to grab "just one" element as opposed to a whole list of elements.  For $z, this obviously doesn't matter, but with x or y you might want to pull out more than one element, as in...

%x = (Mon=>8, Tue=>8, Wed=>7.5, Thu=>8.25, Fri=>7.5, Sat=>2.5, Sun=>0); # hours worked for each day
@weekend_hours = @x{'Sat', 'Sun'}; # hours worked just on weekends

This code takes a slice of the hash containing only the hours worked on Sunday and Saturday and stuffs it into an array.  The @ is necessary in @x{'Sat', 'Sun'} in order to tell Perl that we might want to access several values in our hash (if it were a $, Perl would assume we want just one element, which would cause a pretty serious bug).  Nevertheless, we're still accessing elements of the hash %x (even though the % changes depending on context).

Clear as mud?  (Advice:  Get a Perl book from O'Reilly.)

Offline

#10 2009-09-24 08:25:31

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: [SOLVED] Perl: array as hash key

Thanks for your explanation. I'm already doing a Perl tutorial, so don't worry wink.

Offline

#11 2009-09-24 11:02:02

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: [SOLVED] Perl: array as hash key

SIGTERM wrote:
g0ju wrote:

Do you mean something like that?

@array = ("1", "2");
%hash = ("1" => "foo", "2" => "bar");

print $hash{$array[0]};

Yes, exactly! Is that possible?

I wish I had a picture of my face when I read that. It would have been one of the best "LOLWUT?" macros ever. tongue

@Trent
I thought that was a very good explanation. I'm sure that anyone who comes across this thread will appreciate it.

Last edited by Xyne (2009-09-24 11:03:02)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#12 2009-09-24 13:20:04

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] Perl: array as hash key

Thanks! smile

Offline

Board footer

Powered by FluxBB