Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Array and Hash

by waytoperl (Beadle)
on Nov 23, 2014 at 21:58 UTC ( [id://1108186]=perlquestion: print w/replies, xml ) Need Help??

waytoperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

The content of DATA are modified  %hash value. Need to include  %hash key before or after  SOME_DATA to respective modified  %hash key

Don't know how to search modified  value and include respective  key after or before  SOME_DATA

__HASH__ $VAR1 = { 'Key1' => 'Cat.Mouse.Game.Lion.Tiger.Elephant', 'Key2' => 'Cat.Mouse.Game.Lion.Tiger.Lion', 'Key3' => 'Cat.Mouse.Game.Lion.Tiger.Monkey', };
__DATA__ Pigeon.Lion.Tiger.Elephant SOME_DATA Pigeon.Lion.Tiger.Lion SOME_DATA Pigeon.Lion.Tiger.Monkey SOME_DATA
__Example code__ my %hash = () my $file = 'master.txt'; open (my $OUT_FILE, '<', "TEXT_FILE") or die "ERROR"; while (<$OUT_FILE>) { print "$. \n" if /SOME_DATA/; #searching SOME_DATA and printing }
__OUTPUT__ Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)

UPDATED:

%hash value "Cat.Mouse.Game" is replaced with "Pigeon" and placed in a text file along with SOME_DATA

Ignore "Pigeon" and match other characters. If there is a match, then include respective %hash Key before "SOME_DATA"

Replies are listed 'Best First'.
Re: Array and Hash
by GrandFather (Saint) on Nov 23, 2014 at 22:39 UTC

    Your "question" makes no sense to me (see I know what I mean. Why don't you?). Take your time writing up the issue and try for full sentences. Make your example data as real as you can so important information isn't "lost in translation".

    You mention "modified hash value", but don't mention what modification is acceptable.

    Perl is the programming world's equivalent of English
Re: Array and Hash
by 2teez (Vicar) on Nov 24, 2014 at 02:51 UTC

    Hi waytoperl,

    Considering, the __DATA__ and the hash data presented, there is no way you are getting this desired output

    Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
    Why?
    • If "Pigeon." and " SOME_DATA" is taken away from your input coming from __DATA__, compare with your hash data if "Cat.Mouse.Game." is taken away you practically have these:
      1. Lion.Tiger.Elephant
      2. Lion.Tiger.Lion
      3. Lion.Tiger.Monkey
    • With " SOME_DATA" common to all input, one cannot use it to differentiate, when comparing with the hash data given.
    • And with the OP update given, all compared data will match, except if there is some kind of changed in input data. Then would the (key2) not match
    Something like this:
    use warnings; use strict; my %hash = ( 'Key1' => 'Cat.Mouse.Game.Lion.Tiger.Elephant', 'Key2' => 'Cat.Mouse.Game.Lion.Tiger.Lion', 'Key3' => 'Cat.Mouse.Game.Lion.Tiger.Monkey', ); while ( my $data = <DATA> ) { chomp($data); my $value = join '.' => ( split /[\s.]/, $data )[ 1 .. 3 ]; print map { "$data ($_)\n" if $hash{$_} =~ /$value/ } keys %hash; } __DATA__ Pigeon.Lion.Tiger.Elephant SOME_DATA Pigeon.Lion.Tiger.SeaLion SOME_DATA Pigeon.Lion.Tiger.Monkey SOME_DATA
    OUTPUT
    Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
    Please note that the input data has been changed from what the OP gave.

    With the OP input, the output will be:
    Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Lion SOME_DATA (Key2) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Array and Hash
by Loops (Curate) on Nov 23, 2014 at 22:49 UTC

    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.

Re: Array and Hash
by wjw (Priest) on Nov 23, 2014 at 23:22 UTC

    As close as I can tell, you want to correlate the hash->key to SOME_DATA which resides in __TEXT__FILE__DATA__ using hash->value = string in __TEXT__FILE__DATA__.

    That will be hard to do being that the string in __TEXT__FILE__DATA__ does not match the value you show in hash->key.

    I think I might:

    1. modify all hash->values to get rid of 'Cat.Mouse.Game'
    2. read the text file into a separate hash getting rid of Pigeon as I do so.
    3. step through the values of the hash modified in step one looking for a matching key in the hash from step two
    4. upon match, output the key from hash in step one, the value from hash in step two along with its key.
    If I understood the desired output, that should give it to you(though key first), and should run pretty fast too.

    Hope that helps....

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is simply an inconvenient fact

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108186]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (None)
    As of 2024-04-25 01:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found