You are not logged in.
Pages: 1
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
what do you want to get ? hash of arrays ?
Zygfryd Homonto
Offline
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
Do you mean something like that?
@array = ("1", "2");
%hash = ("1" => "foo", "2" => "bar");
print $hash{$array[0]};
Offline
Do you mean something like that?
@array = ("1", "2"); %hash = ("1" => "foo", "2" => "bar"); print $hash{$array[0]};
Yes, exactly! Is that possible?
Offline
What else do you want to do? Isn't that what you wanted?
Offline
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 !
Offline
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
@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
Thanks for your explanation. I'm already doing a Perl tutorial, so don't worry .
Offline
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.
@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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Thanks!
Offline
Pages: 1