Hello. You lost me a bit about the mapping between the input text file and the hash. The values don't match and yet you want Key1 and Key3 in your output? I'm going to assume that was a typo, and what you're asking is to match that first string on each input line to the values contained in your hash:
my %hash = ( Key1 => 'Pigeon.Lion.Tiger.Elephant',
Key2 => 'Pigeon.Lion.Tiger.Rabbit',
Key3 => 'Pigeon.Lion.Tiger.Monkey',
);
my %lookup = reverse %hash;
while (<DATA>) {
chomp;
my ($value) = split;
my $key = $lookup{$value};
print "$_ ($key)\n" if /SOME_DATA/ and defined $key;
}
__DATA__
Pigeon.Lion.Tiger.Elephant SOME_DATA
Pigeon.Lion.Tiger.Lion SOME_DATA
Pigeon.Lion.Tiger.Monkey SOME_DATA
Prints:
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1)
Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
Hopefully that looks right to you. The main hash is reversed so we can use %lookup to find matching values and discover the associated key. Reverse will put the values into the keys, with their respective keys as values. Everything else falls into place.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|