Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash tables

Status
Not open for further replies.

NetDiver030

Programmer
Joined
Jul 26, 2003
Messages
1
Location
DE
ok .. here a little challenge ..

%bla->{"key1", "keya"} = "a";
%bla->{"key1", "keyb"} = "b";
%bla->{"key2", "keyc"} = "c";
%bla->{"key2", "keyd"} = "d";

looping through all alvailable keys is easy.
while(($k,$v)=each(%bla)) {...}

Does anyone know a smart way to loop only through all keys, that have "key2" in the first place without looping through all data sets and splitting the line of keys ?

the output should be
c d
and a loop count should be 2 not 4.
(As you can imagine I have to process more than 4 data sets ;-) )

All the best ...

Marco
 
Code:
#!/usr/bin/perl -w

use strict;

my $foo = { 'key1' => [{'keya' => 'a'}, {'keyb' => 'b'}],
            'key2' => [{'keyc' => 'c'}, {'keyd' => 'd'}],
                };

foreach my $item(@{$foo->{'key2'}})
{
     while(my ($key, $val) = each %{$item}) { print "$val "; }
}

print "\n";
exit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top