Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

correlation between two inputt files

by himanshuyadav
on Apr 27, 2012 at 12:02 UTC ( #967606=perlquestion: print w/ replies, xml ) Need Help??
himanshuyadav has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Can you please help me in following scenario in Perl scripting? I want to correlate two text files to get output.

CONSIDER TWO CHEMICAL REACTION:-

 (1)     A+E+C--->F+H+L  (2)       A+B--->G+I  
INPUT_FILE1.TXT #REACTANT #REACTION NO. A 1 E 1 C 1 A 2 B 2
INPUT_FILE2.TXT #PRODUCT #REACTION NO. F 1 H 1 L 1 G 2 I 2

AND THE OUTPUT SHOULD COME OUT AS:-

reactant: E, product: F reactant: E, product: H reactant: E, product: L reactant: C, product: F reactant: C, product: H reactant: C, product: L reactant: A, product: F reactant: A, product: H reactant: A, product: L reactant: A, product: G reactant: A, product: I reactant: B, product: G reactant: B, product: I

i.e.,we want to correlate each reactant to each product in it's corresponding reaction.

Can someone please send me the code.Urgently needed

Comment on correlation between two inputt files
Select or Download Code
Re: correlation between two inputt files
by choroba (Abbot) on Apr 27, 2012 at 12:36 UTC
    This is how I would proceed:

    Create a hash. The keys will be the reaction numbers. The values will be hash references with two keys: 'reactants' and 'products'. The values will be hash references again, use reactants and products respectively as keys. The values can stay undefined. After the hashes are filled, just go over the keys and print out all the tuples in nested loops.

      Dear monk,

      thank you for your reply.

      can you please code this in perl.I am not a perfectionist programmer in perl.Please help me out.

      waiting for your reply.

        Feel free to contact me to arrange payment...

        #!/usr/bin/env perl use Modern::Perl; my @p; open my $fd2, '<', 'INPUT_FILE2.TXT' or die $!; while(<$fd2>){ next if /#/ or ! /\w/; my( $p, $n ) = split; push @{$p[$n]}, $p; } close $fd2; open my $fd1, '<', 'INPUT_FILE1.TXT' or die $!; while(<$fd1>){ next if /#/ or ! /\w/; my( $r, $n ) = split; say "reactant: $r, product: $_" for @{$p[$n]}; } close $fd1;

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Re: correlation between two inputt files
by jethro (Monsignor) on Apr 27, 2012 at 12:38 UTC

    Just create a HashOfArrays, with reaction number as key and an array of products as data. Fill it with the data from File2.txt. Then read in the File1.txt and for every reactant check the hash for the array of products it can be transformed into.

Re: correlation between two inputt files
by Anonymous Monk on Apr 27, 2012 at 12:40 UTC
Re: correlation between two inputt files
by MidLifeXis (Prior) on Apr 27, 2012 at 12:53 UTC

    If I am reading your question correctly, you wish to join two files where they have common values in the second column (see join for the unix command line equivalent).

    --MidLifeXis

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-10-23 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls