CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Accessing hash with dynamic keys

 



hem
User

Feb 22, 2012, 9:25 AM

Post #1 of 9 (1704 views)
Accessing hash with dynamic keys Can't Post

I have a question . Its hard to explain but i will give it a shot

I have a hash (%Hash) and the number of keys and hash structure are read from a file
Example : File will have some like this
1. HashElements- A:B
2. HashElements- C:D:E

If i read line 1 my hash should be $hash{A}{B}
If i read line 2 my hash should be $hash{C}{D}{E}

I read the HashElemenst from the file and parse the hash elements in a array. For line one i have a @Array with A and B as index and so on

But i don't know how to access the hash since the number of keys might be different every time

I apologize if the question isn't clear

Thanks in advance




Code



      
    


BillKSmith
Veteran

Feb 22, 2012, 11:35 AM

Post #2 of 9 (1694 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

I suspect that you do not understand what a hash does. A hash associates a value with each index. The value may be a reference to another hash. There is no such thing as a "hash element".

Your expressions "$hash{A}{B}" and "$hash{C}{D}{E}" are each a shorthand way of accessing one value (undefined in your example) from a nested hash. There is no general expression which will include both of these.

Nested hashes a probably not a good way to represent your data. Tell us more about your application and we can help you design a better data structure.
Good Luck,
Bill


rovf
Veteran

Feb 23, 2012, 1:35 AM

Post #3 of 9 (1679 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

Building the hash keys in order is not so difficult, but what *values* do you want to store in the hash? Or are you only interested in the presence/absence of the keys?


hem
User

Feb 23, 2012, 8:21 PM

Post #4 of 9 (1646 views)
Re: [rovf] Accessing hash with dynamic keys [In reply to] Can't Post

Problem is that each key has a different depth

I want to get to the final value


rovf
Veteran

Feb 24, 2012, 5:15 AM

Post #5 of 9 (1638 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

I don't understand what you mean by "final value". Maybe you can sketch for a certain set of input, how your hash is supposed to look like?


naven8
Novice

Feb 24, 2012, 6:27 AM

Post #6 of 9 (1634 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

I strictly dont recommend the following code.It will work only when you have only 1 key for the inner hash.
All elements are seperated by "END" in the array.

Code
use strict;  
use warnings;
my @arr;
my %hash = ('a0' => {'b0' => 'c0',},
'a1' => 'b1',
'a2' => {'b2' => {'c2' => 'd2'},},
);

my $hash_ref = \%hash;

foreach my $ke (sort(keys(%hash))){
func($hash_ref,$ke);
}

sub func {
my $ref =shift;
my $ke = shift;

if (ref($ref->{$ke}) =~ /HASH/){
push(@arr,$ke);
foreach my $ke_2 (sort(keys(%{$ref->{$ke}}))){
func(\%{$ref->{$ke}},$ke_2);
}

}
else{
push (@arr,$ke);
push (@arr,$ref->{$ke});
push (@arr,"END");
return;
}
}

print "@arr";



(This post was edited by naven8 on Feb 24, 2012, 6:28 AM)


BillKSmith
Veteran

Feb 24, 2012, 10:34 AM

Post #7 of 9 (1627 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

Do you mean that you have an existing hash and you wish to access the value specified by the keys in your data file? If this is the case, it is easier to treat the different cases separately.


Code
use strict; 
use warnings;
use Readonly;
Readonly::hash my %hash => (
A => {
B => 'value_AB',
},
C => {
D => {
E => 'value_CDE',
},
},
);

while (my $line = <DATA>) {
my $value;
if ( $line =~ /[^:]([A-Z]):([A-Z])[^:]/) {
$value = $hash{$1}{$2};
}
elsif ( $line =~ /[^:]([A-Z]):([A-Z]):([A-Z])[^:]/ ) {
$value = $hash{$1}{$2}{$3};
}
print $value, "\n";
}
__DATA__
. HashElements- A:B
2. HashElements- C:D:E

Good Luck,
Bill


hem
User

Feb 24, 2012, 10:56 AM

Post #8 of 9 (1626 views)
Re: [BillKSmith] Accessing hash with dynamic keys [In reply to] Can't Post

Bill

That's how i am doing it today but i number of keys can vary every time So it will fail if i don't have the exact number in elsif

Thanks


BillKSmith
Veteran

Feb 24, 2012, 12:10 PM

Post #9 of 9 (1621 views)
Re: [hem] Accessing hash with dynamic keys [In reply to] Can't Post

True, but notice. My first regular expression matches any input line with exactly two keys. The if then executes the hash reference for any two-key value. Likewise, the second regular expression matches any input line with exactly three keys. The elsif executes the hash reference for any three-key value. If you have input lines with more keys, you do need more elsif's. The total number of if (or elsif) blocks needed is one less than the number of keys on the record which has the most.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives