Re: HoHoH
On 1/9/07, oryann9 [EMAIL PROTECTED] 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. [snip] 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? Yup, three loops. I'll get you started: for my $name (keys %dub_hash) { for my $uid (keys %{$dub_hash{$name}}) { # do stuff, note the %{...} above to force precedence } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: HoHoH
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/
Re: HoHoH
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? Yup, three loops. I'll get you started: for my $name (keys %dub_hash) { for my $uid (keys %{$dub_hash{$name}}) { # do stuff, note the %{...} above to force precedence } } Ok thank you! So if I have a HoHoHoH I would need 4 loops and so on? Is there a good tutorial for hashes with real world examples b/c I seem to struggle with comples data structures as the above took me longer than expected. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Re: HoHoH
Dave Gray wrote: Yup, three loops. I'll get you started: for my $name (keys %dub_hash) { for my $uid (keys %{$dub_hash{$name}}) { # do stuff, note the %{...} above to force precedence Parentheses () are used for precedence, %{} dereferences the reference contained in $dub_hash{$name} } } 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/