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.
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.
To access single element of hash, ($) sign is used before the variable name. And then key element is written inside {} braces.
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.
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.
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.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With