Yea, this looks to be it. My code with be simplified since each hash key will always represent 2 pieces of data (stored in an array).
Thanks again, Jason -----Original Message----- From: $Bill Luebkert [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 29, 2004 4:37 PM To: Allison, Jason (JALLISON) Cc: [email protected] Subject: Re: Hash with multiple keys? Allison, Jason (JALLISON) wrote: > Thanks all for the replies. > > My understanding is that two dimentional objects are usefull when > $key1 (1 row) represents an unique identifier applied to $key2 (1 > column) to 'point' to one piece of data (cell), if we were looking at > a two dimensional object (flat spreadsheet). > > What I am looking for (and I think it was hinted in a couple emails) > is a way to store 2 pieces of data that both have the same key. This > can be accomplished a number of ways, I guess it comes down to me > wondering which is the 'perl' way. And if you cant tell, I am a new > perl convert. Some examples of what I mean: > > my %data1_hash; my %data2_hash; my %unique_hash; my %my_hash; > > my $identifier = 'unique'; > my $data1 = 'a'; > my $data2 = '2'; > > # 2 hashes > $data1_hash{$unique} = $data1; > $data2_hash($unique) = $data2; > > # joined data with common delimeter, need to split out when want to > access $key1, $key2 > $unique_hash($unique) = join('|', $data1, $data2); Why not just array the values as in unique1 below (unique2 would be the normal scalar method) ? my %unique_hash; $unique_hash{unique1} = ['data1', 'data2']; $unique_hash{unique2} = 'data3'; foreach my $key (keys %unique_hash) { if (ref $unique_hash{$key} eq 'ARRAY') { print "$key => (\n"; foreach (@{$unique_hash{$key}}) { print "\t$_\n"; } print ")\n"; } else { print "$key => $unique_hash{$key}\n"; } } __END__ > # What I was tring to do. Hoping there was an easy way to pull out > both $unique and $data1 # via some 'keys' call. I guess this could be > done with a join and split, was wondering if there > # was another way. > $my_hash($unique, $key1) = $key2 > > foreach $key (keys(%my_hash)) { > my ($value1, value2) = // split $key > > Thanks, > Jason > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Don > VanSyckel > Sent: Wednesday, December 29, 2004 2:48 PM > To: [email protected] > Subject: Re: Hash with multiple keys? > > > The hash you listed has one key comprised to the values of two > variables. I believe you meant to use > > $hash{$key1}{$key2} = $data; > ... > forash $key1 (sort keys %hash) > { foreach $key2 (sort keys %{$hash{$key1}}) > { print "key1 = $key1\tkey2 = $key2\n"; > } > } > > Don > > > Allison, Jason (JALLISON) wrote: > >>Sorry for the horribly lame question, but I am not having any luck >>finding an answer, probably because I don't know how to phrase the >>question properly. >> >>When I have a hash defined by multiple keys, how can I pull both keys >>out? >> >>$hash{$key1, $key2} = $data; >>... >>foreach $key (sort(keys(%hash))) { >> # here ?? >> my ($key1, $key2) = split(/ /, $key, 2); >>} >> -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
