Ask a Question related to PERL Beginners, Design and Development.
-
Trina Espinoza #1
multidimentional hash that has both two and three keys
Can you have a multidimentional hash that has both two and three keys?
For example:
Array with two keys and a value:
$HASH-> {$VAR}{letter} ="a";
Same Array with three keys and a value:
$HASH->{$VAR}{$number){float} = "1.1";
$HASH->{$VAR} {$number}{integer}="2";
Is this possible? I'm not sure if it didn't wok b/c of an error in syntax or
if it's because it's not possible.
To print the two keys and value I know I do:
print $HASH->{$VAR}{letter};
BUT, if I have three keys and a value, how do I print it? If $number is a
different number every time (1 ..10), how do I get it to print so that it
iterates through each number and gives me the float and the integer for
each.
Hope this makes sense! Any examples would help get me started
Thanx!
__________________________________________________ _______________
High-speed users—be more efficient online with the new MSN Premium Internet
Software. [url]http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1[/url]
Trina Espinoza Guest
-
DBI::mysql column names as hash keys?
Hello, Given a DBI::mysql database with 2 tables and 5 columns like so: human --> name --> age --> sex dog --> breed --> colour -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Detecting duplicate keys in a hash I am "requiring"
Hi, I have a configuration file which is in a perl hash (of hashes and arrays) format. $hash = { key1 => { key2 => val2, key3 => val3, },... -
Another reference question (hash of hash references)
beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes...... -
Using slices with 'my' to initialize hash keys and values.
I have the following piece of code which works (meaning, does what I expect): #!/usr/bin/perl use strict; use warnings; my %translation; -
Rob Dixon #2
Re: multidimentional hash that has both two and three keys
Trina Espinoza wrote:
That closing ')' should be a '}'.>
> Can you have a multidimentional hash that has both two and three keys?
>
> For example:
>
> Array with two keys and a value:
> $HASH-> {$VAR}{letter} ="a";
>
> Same Array with three keys and a value:
> $HASH->{$VAR}{$number){float} = "1.1";
Yes, it's certainly possible. What error did you get?> $HASH->{$VAR} {$number}{integer}="2";
>
> Is this possible? I'm not sure if it didn't wok b/c of an error in syntax or
> if it's because it's not possible.
Something like this, below?>
> To print the two keys and value I know I do:
>
> print $HASH->{$VAR}{letter};
>
> BUT, if I have three keys and a value, how do I print it? If $number is a
> different number every time (1 ..10), how do I get it to print so that it
> iterates through each number and gives me the float and the integer for
> each.
use strict;
use warnings;
my $HASH = {};
my $VAR = 'key';
my $number = 99;
$HASH->{$VAR}{letter} = "a";
$HASH->{$VAR}{$number}{float} = "1.1";
$HASH->{$VAR}{$number}{integer} = "2";
my $hash = $HASH->{$VAR};
foreach my $key (keys %$hash) {
my $value = $hash->{$key};
if ($key eq 'letter') {
printf "%s => letter %s\n", $key, $value;
}
else { # $key is a hash reference
printf "number %s => float %s, integer %s\n",
$key, $value->{float}, $value->{integer};
}
}
**OUTPUT
letter => letter a
number 99 => float 1.1, integer 2
I'm not completely clear on your problem, but I hope this gets you going.> Hope this makes sense! Any examples would help get me started
Cheers,
Rob
Rob Dixon Guest



Reply With Quote

