Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - How to find the key of a hash if you know the value?

Tags:

hash

perl

What I am trying to do is retrieve the key of a key value pair in a hash because all I have from a file I am reading in is a value.

The code produces something like this:

12345 welcome.html

The code for this part is:

my %bugs;
my $bug;
open(FH, '-|', "lynx -dump '$queryurl'") or die "Could not lynx $queryurl: $!";
while (<FH>)
{
    if (/<bz:id[^>]*>([^<]*)</)
    {
        $bug = $1;
    }
    if (/<bz:url[^>]*>([^<]*)</)
    {
        my $url = $1;
        $bugs{$url} = $bug;
        $bug = undef;
    }
}
close(FH);

# for debugging purposes
foreach my $bug (keys %bugs)
{
    print "$bugs{$bug} $bug\n";
}
exit;

Then, somewhere else in a file called bad.txt I get output like:

Documents that failed: daerror 6 0 6 welcome.html

The code for reading this file is :

my $badfile = "$dir/bad.txt";
open(FH, "<$badfile") || die "Can not open $badfile: $!";
# ignore first line
<FH>;
while (<FH>)
{
    chomp;
    if (!/^([^ ]+) [^ ]+ [^ ]+ [^ ]+ ([^ ]+) [^ ]+$/)
    {
        die "Invalid line $_ in $badfile\n";
    }
    my $type = $1;
    my $testdoc = $2;
}

But I already have the filename extracted from this using a regular expression.

like image 956
martincarlin87 Avatar asked Jun 21 '11 10:06

martincarlin87


People also ask

How get key from hash value in Perl?

Extracting Keys and Values from a Hash variable The list of all the keys from a hash is provided by the keys function, in the syntax: keys %hashname . The list of all the values from a hash is provided by the values function, in the syntax: values %hashname . Both the keys and values function return an array.

How do I view a hash element in Perl?

To access single element of hash, ($) sign is used before the variable name. And then key element is written inside {} braces.

How do I print a hash key in Perl?

We can done this by using map function. map {print "$_\n"} keys %hash; map function process its statement for every keys in the hash. The map function is there to transform a list.

What is key and value in hash?

The key is sent to a hash function that performs arithmetic operations on it. The result (commonly called the hash value or hash) is the index of the key-value pair in the hash table.

How to check if a key exists in a Perl hash?

Next, we use the Perl exists function to see if the key exists in our Perl hash: As you can see from the code, the hash key coke does indeed exist, so the Perl print statement shown will be executed. Of course this example is very simple, but I hope it helps to demonstrate the Perl exists function in combination with a key in a Perl hash.

How to search for a given key in a hash?

You can use the Perl exists function to see if a key can be found in a hash. Here's the general case of how to search for a given key in a hash:

How to get a list of all keys and values in Perl?

You can get a list of all of the keys from a hash in Perl by using keys function, which has the following syntax − This function returns an array of all the keys of the named hash. Following is the example − This will produce the following result − Similarly, you can use values function to get a list of all the values.

How to get a particular value from a hash?

Note that if we want to access a particular value then we should know the appropriate key of that value and if the key is not known then we need to use the key function to access the hash to get the entire list of keys and then we can iterate over the keys to find the particular value.


1 Answers

You can make an inverted copy of your original hash with reverse operator and then make a "normal" lookup (would work properly only if values in original hash are unique).

More on this topic including handling duplicate values at perlfaq4: How do I look up a hash element by value

like image 185
Roman Grazhdan Avatar answered Nov 15 '22 06:11

Roman Grazhdan