For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > creating a hash with a set of keys that are contained within an array.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating a hash with a set of keys that are contained within an array.
terry.mah01@gmail.com

2005-10-21, 6:56 pm

Hello,
I was wondering if it was possible to create, assign, and de-reference
a hash with a set of hash keys that are contained within an array:
#-------------------------------------------------------
#!/usr/bin/env perl
use strict;

my @hashKeys = = qw('KEY1', 'KEY2');
my %hashArr;

$hashArr{$hashKeys[0]}{$hashKeys[1]} = 'Hello World';
print $hashArr{$hashKeys[0]}{$hashKeys[1]} . "\n";

#-------------------------------------------------------

How do I assign a value to the hash if I don't know how many elements
are in my array?

How do I de-reference my hash if I don't know how many elements are in
my array?


Thanks for the assistance.

tmah01

2005-10-21, 6:56 pm

Sorry for the typos, my cut-and-paste is flakey:

#-----------------------------------------------------------
#!/usr/bin/env perl
use strict;

my @hashKeys = qw(KEY1, KEY2);
my %hashArr;

$hashArr{$hashKeys[0]}{$hashKeys[1]} = 'Hello World';
print $hashArr{$hashKeys[0]}{$hashKeys[1]} . "\n";
#-----------------------------------------------------------

Paul Lalli

2005-10-21, 9:55 pm

terry.mah01@gmail.com wrote:
> Hello,
> I was wondering if it was possible to create, assign, and de-reference
> a hash with a set of hash keys that are contained within an array:
> #-------------------------------------------------------
> #!/usr/bin/env perl
> use strict;
>
> my @hashKeys = = qw('KEY1', 'KEY2');
> my %hashArr;
>
> $hashArr{$hashKeys[0]}{$hashKeys[1]} = 'Hello World';
> print $hashArr{$hashKeys[0]}{$hashKeys[1]} . "\n";
>
> #-------------------------------------------------------
>
> How do I assign a value to the hash if I don't know how many elements
> are in my array?
>
> How do I de-reference my hash if I don't know how many elements are in
> my array?


Why are you creating a data structure that you know will be difficult,
if not impossible, to actually use? Don't go through hoops to make
your code work with a particular data structure. Define your data
structure to be most useful to your code.

Perhaps you could tell us what you're actually *trying* to do?

Paul Lalli

wisefamily@integrity.com

2005-10-22, 6:55 pm

tmah01 wrote:
> Sorry for the typos, my cut-and-paste is flakey:
>
> #-----------------------------------------------------------
> #!/usr/bin/env perl
> use strict;
>
> my @hashKeys = qw(KEY1, KEY2);
> my %hashArr;
>
> $hashArr{$hashKeys[0]}{$hashKeys[1]} = 'Hello World';
> print $hashArr{$hashKeys[0]}{$hashKeys[1]} . "\n";
> #-----------------------------------------------------------


If you are trying to do

$hashArr{$hashKeys[0]}{$hashKeys[1]} = 'Hello World';

or

$hashArr{$hashKeys[0]}{$hashKeys[1]}{$ha
shKeys[2]} = 'Hello World';

etc. based on how many values are in @hashKeys, then this is what you
do:

#!/usr/bin/env perl -w
use strict;

my @hashKeys = qw(KEY1 KEY2);
my %hashArr;
my $value = 'Hello World';
my $ref = \%hashArr;
my $i;

##### Assigning the value #####

for($i = 0; $i < $#hashKeys; $i++) { # Skip the last item
$ref{$hashKeys[$i]} = {}; # Create an anonymous hash and assign it
to $hashArr{$key}
$ref = $ref{$hashKeys[$i]}; # Assign the newly created anonymous
hash to $ref
}

$ref{$hashKeys[-1]} = $value; # Assign $value to the hash with the last
key

##### Dereferencing the hash #####

$ref = \%hashArr;

for($i = 0; $i < $#hashKeys; $i++) { # Skip the last item
$ref = $ref{$hashKeys[$i]}; # Assign the next hash to $ref
}

$value = $ref{$keys[-1]}; # Get the value of the last key

print "$ref{$keys[-1]}\n"; # Prints "Hello World\n";

Note that I included warnings in the shebang line (You could also do
this by inserting the line "use warnings;".):

#!/usr/bin/env perl -w

Also the qw// operator doesn't require commas:

my @hashKeys = qw(KEY1 KEY2);

$ref holds the hash for each repetition of the loop as we work our way
up the data structure. We loop once for every value in @hashKeys but
the last one as we build the data structure. Finally, we have another
loop that loops once for every value in @hashKeys but the last one, and
after that, we retrieve the value.

Hope this helps,
David

tmah01

2005-10-24, 3:55 am

YES! This is exactly what I was looking for. Thanks!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com