in reply to Find the word in Array, Hash, Scalar

Hello

Everything in foreach

@array_1 = ("cat", "rat", "lion"); @array_2 =("dog", "tiger"); $variable="horse"; %hash=("1" => 'peacock', "2" => 'duck') ; foreach (@array_1, @array_2, $variable, @hash{keys %hash}) { if ($_ eq "tiger") { print "\nfound $_"; } }

Thanks

Gopal.R

Replies are listed 'Best First'.
Re^2: Find the word in Array, Hash, Scalar
by Animator (Hermit) on May 11, 2005 at 09:46 UTC

    @hash{keys %hash} really is a bad idea...

    What will this do? it will create a list of all the keys in the hash, use that list in a hash slice and return all values.

    If you want to get all values of a hash then there is the values function... it is shorter, cleaner and more efficient.

    (Another possibilty would ofcourse be to use kapproach's approach, which is probably better)

Re^2: Find the word in Array, Hash, Scalar
by Forsaken (Friar) on May 11, 2005 at 09:11 UTC
    Or, to expand on the parent poster's suggestion, stuff it all in a big array and then grep on it.

    Remember rule one...