in reply to question about what to grep(?)

I think you need to provide a bit more detail about the keys of your hash. As pointed out, they cannot be arrays.

Apart from that your approach seems to make sense. The something in your grep would be some condition to select some keys from your hash.

The best approach to print depends on the values of your hash. @haystack{@wants} gives you an array of the values of the selected keys. You have not told us what these values are. They could be simple numbers or complex data structures and you would need to taylor your approach to that. Data::Dumper should always give you a printout but the question is whether it is sufficient for your purpose.

Replies are listed 'Best First'.
Re^2: question about what to grep(?)
by crunch_this! (Acolyte) on Apr 12, 2013 at 15:14 UTC

    I've gone with hdb's suggestion in the other node with the nested foreach loops but I simplified the hash definition to

    $haystack{"$a, $b, $c"} =  \@zeros;

    I reversed the hash after reading that in perl, hashes are optimized for searching the keys, so now the \@zeros are the keys. But they're arrays, at least in my head. I'm used to thinking in terms of vectors, sets of ordered pairs, etc & not strings or whatever.

    I'm interested in the keys that only contain integers so the "something" I was thinking of using a regex like this

    / (.*\.0000.*)|(.*\.9999.*) /

    because that would find floats with the right form. It's just I'm not sure what to put in the grep to make sure I'm getting only @zeros that have ALL integers inside & not just at least one. I wouldn't hve thought to use more than one grep like Don Coyote though. Maybe I need to learn more about grep... & then after finding a slice with the right keys, print out the corresponding values. But it's the grep part that I'm stuck on.

      I do not think that reversing the hash helps. In any case you should define a function that returns true (ie 1) if the argument is (approximately) an integer and then do a grep on the \@zeroes.

      # untested sub is_approximately_an_integer { my $x = shift; my $eps = 0.0001; return abs( $x-round($x) ) < $eps; }

        I got an uninitialized variable error the way you had it so I added the foreach bit here, & also changed the round to int(). I got your idea:

        sub is_approximately_an_integer { foreach my $x ( @zeros ) { $x = shift; my $eps = 0.0001; return abs( $x-int($x) ) < $eps; } }

        It seems to work ok like that. I guess next is to use the subroutine with $_ inside grep somehow?

        my @wants = grep {  } values %haystack;