oryann9 wrote:
> I have a HoHoH structure that looks like after running print Dumper (\%hash);
>
> 'prlhNSSA' => {
> '1499' => {
> 'gecos' => 'First Name Last Name,CIS,location,
> 'host' => '/var/tmp/passwd.tgpdrpp1.hpux',
> 'gid' => '205'
> }
> }
> };
>
>
> My 1st hash is keyed by name, that accesses 2nd hash keyed by UID, that
> accesses a 3rd hash keyed by remaining items: gid,gecos and hostname.
>
> The snippet of code is:
>
> my %hash;
> my ($name, $uid, $gid, $gecos);
> my $regexp = qr/(\w+|\w+\.\w+|\w+\-\w+|\w+\_\w+)\s+(\d+)\s+(\d+)\s+(.*)/i ;
> ...
> ...
> elsif (/$regexp/) {
> ($name, $uid, $gid, $gecos) = ($1, $2, $3, $4);
> $hash{$name}{$uid} = { # User data keyed by uid
> gid => $gid,
> gecos => $gecos,
> host => $host,
>
> my question is how do I access and print all values? I am trying:
>
> foreach (keys %dub_hash) {
> print %{$dub_hash->{$name}->{$uid}->{%gid}->{$gecos} };
> }
>
> Do I need three loops? What am I doing wrong?
Yes, you need three loops:
$ perl -le'
my %hash = (
prlhNSSA => {
1499 => {
gecos => "First Name Last Name,CIS,location",
host => "/var/tmp/passwd.tgpdrpp1.hpux",
gid => 205,
},
},
);
for my $name ( keys %hash ) {
for my $uid ( keys %{ $hash{ $name } } ) {
for my $key ( keys %{ $hash{ $name }{ $uid } } ) {
print $hash{ $name }{ $uid }{ $key };
}
}
}
'
First Name Last Name,CIS,location
205
/var/tmp/passwd.tgpdrpp1.hpux
Or, since you just want the values:
$ perl -le'
my %hash = (
prlhNSSA => {
1499 => {
gecos => "First Name Last Name,CIS,location",
host => "/var/tmp/passwd.tgpdrpp1.hpux",
gid => 205,
},
},
);
for my $uid ( values %hash ) {
for my $hash ( values %$uid ) {
for my $val ( values %$hash ) {
print $val;
}
}
}
'
First Name Last Name,CIS,location
205
/var/tmp/passwd.tgpdrpp1.hpux
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/