in reply to Write in files

heyho,

I'm about to solve that Problem. At least i think i'll do.

My Problem now is, that i have to search threw the values of %Verzeichnis. And if the Skalar und the value are eq,the key of the value should be written into a file.

Maybe there is some function to search threw the Hash Values? And to find out the key for the value?

Sorry, but i have not worked with Hashes so much, yet.

Thank you!

Replies are listed 'Best First'.
Re^2: Write in files
by hdb (Monsignor) on Nov 20, 2013 at 14:21 UTC

    You can use reverse to reverse a hash: keys become values, values become keys. One has to be careful though, if there are duplicate values (two persons sharing a phone number) only one entry will survive. In addition, if the values are not strings, Perl will stringify them, which can have funny effects.

    You can also use grep like this:

    my @keys = grep { $hash{$_} eq $some_value } keys %hash;

    which can return multiple values keys.

    Update: some example

    my %verzeichnis = ( peter => 12345, paul => 34567, mary => 12345 ); my @names = grep { $verzeichnis{$_} eq '12345' } keys %verzeichnis; print "@names\n"; my %rueckwaertssuche = reverse %verzeichnis; print $rueckwaertssuche{'12345'}, "\n"; # lost mary