perlbeginner10 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a hash, and I want to print those values that are greater than, for example, 1. My code is as follows:
for my $key (keys %hashs) { if($hashs{$key}{$_} > 1 ) print "$key - $hashs{$key}{$_} -> $_\n" for keys %{$hashs{$key +}} ; }
But it gives the error message:
Use of uninitialized value in numeric gt (>) at test.pl line 33, <$tes +tdataset> line 7.
Can you please tell me how to correctly print? The values of the hash is either 1 or greater than 1.
Thanks

Replies are listed 'Best First'.
Re: printing Hash
by wfsp (Abbot) on Mar 25, 2006 at 13:02 UTC
    Nearly!

    #!/usr/bin/perl use warnings; use strict; my %hash = ( one => 1, two => 2, ); for my $key (keys %hash) { if ($hash{$key} > 1 ){ print "$key - $hash{$key}\n"; } } print '-' x 20, "\n"; # or, using the magic variable $_ for (keys %hash) { if ($hash{$_} > 1 ){ print "$_ - $hash{$_}\n"; } } __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl two - 2 -------------------- two - 2 > Terminated with exit code 0.
    You have a hash but you're trying to treat it as a hash of hashes ($hash{$key}{$_}).

    You're also trying to loop over the hash twice (the second for).

    You also need a braces around your if block.

    If you do indeed have a hash of hashes to loop over show us a sample of your data and we'll have a go at that too.

Re: printing Hash
by bart (Canon) on Mar 25, 2006 at 13:16 UTC
    You put that inside out, you're testing $_ before you have a value for it. Properly ordered, with the for and the if swapped, the code becomes:
    for my $key (keys %hashs) { for (keys %{$hashs{$key}}) { print "$key - $hashs{$key}{$_} -> $_\n" if $hashs{$key}{$_} > 1; } }
      Hi
      In the code that you suggested, I HAVE to include the : for keys %{$mappings{$key}}; at the end of print statement.
      I did this:
      for my $key (keys %mappings) { print "$key - $mappings{$key}{$_} -> $_\n" for keys %{$ma +ppings{$key}} if ($mappings{$key}{$_} > 1); }
      But the code still doesn't work.
      The error message is:
      syntax error at test.pl line 33, near "} if"

      Thanks
        You can't put two "modifiers" in a single statement. That's what you're trying to do, and that's what perlsyn says you can't do — unfortunately.
        Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending).
        I didn't even need to stress the word "single" myself, it's written like that in the docs.
Re: printing Hash
by ahmad (Hermit) on Mar 25, 2006 at 13:01 UTC

    Hello ,

    i didn't understand what you're trying to accomplish here , but this may help you out

    my %HASH = ('Key' => 1,'Other' => 0 , 'Nothing' => 3); while (my ($key,$value) = each %HASH) { if($value > 1){ print "Found Key : $key with a value : $value \n"; } }
Re: printing Hash
by njcodewarrior (Pilgrim) on Mar 25, 2006 at 13:14 UTC

    Try this:

    #! /usr/bin/perl use strict; use warnings; my %hashs = ( 'key_a' => 1, 'key_b' => 0, 'key_c' => 5, 'key_d' => 2, 'key_e' => 4, ); foreach my $key ( sort keys %hashs ) { if ( $hashs{$key} > 1 ) { print "$key -> $hash{$key} is greater than 1\n"; } else { print "$key -> $hash{$key} is less than or equal to 1\n"; } }

    This outputs:

    key_a -> 0 is less than or equal to 1 key_b -> 1 is less than or equal to 1 key_c -> 5 is greater than 1 key_d -> 2 is greater than 1 key_e -> 4 is greater than 1

    The default perl variable in loops is $_, so if you don't explicitly assign it to a variable ( my $keys in this case), then you would access the keys of the hash using $_. Since you have assigned $_ to $keys, you access them using this variable name.
    Hope this helps.

    --njcodewarrior
Re: printing Hash
by davidrw (Prior) on Mar 25, 2006 at 14:26 UTC
    I was suprised to see all the above responses w/o the mention of grep (though gube's and sh1tn 's are essentially grep's)
    while( my ($key, $hash) = each %hashs ){ foreach my $subkey ( grep { $hash{$_} > 1 } keys %$hash ){ printf "%s - %s -> %s\n", $key, $hash{$subkey}, $subkey; } }
Re: printing Hash
by sh1tn (Priest) on Mar 25, 2006 at 13:59 UTC
    In addition, use next:
    for ( keys %hash ) { next if not $_ > 1; ... }


Re: printing Hash
by gube (Parson) on Mar 25, 2006 at 13:23 UTC

    Hi try this,

    #! /usr/bin/perl my %HASH = ('Key' => 1,'Other' => 0 , 'Nothing' => 3); map { $HASH{$_} > 1 ? print $_ : '' } keys(%HASH);
    output : Nothing