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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi rashichauhan,

Code is not working.A new file is genertaed but consist of only last element of first file.Plz help me in this regard.

Use an hash and cut the chase on using for loops.
Open the first file and read line by line, then on each line split on space, use those as your key/value of the hash.Then open the second file, step through it, one after the other, splitting on each line too. Then use the word to check if such exist in your hash, if so print it to the file you want and if not print it to another one.

Something like this will do.

#!/usr/bin/perl -l use warnings; use strict; use Inline::Files; my %file1; while (<FILE1>) { next if /^\s+$/; my ( $key, $value ) = split; $file1{$key} = $value; } my $str_union; my $str_inter; while (<FILE2>) { next if /^\s+$/; my ( $key, $value ) = split; if ( $file1{$key} ) { $str_union .= $_; } else { $str_inter .= $_; } } print "This goes to File 3\n", $str_union, "\nThis goes to File 4\n", $str_inter; __FILE1__ EC:1.1.1.42 isocitratedehydrogenase EC:1.1.1.44 6-phosphogluconatedehydrogenase EC:1.1.1.49 glucose-6-phosphate1-dehydrogenase __FILE2__ EC:1.1.1.42 isocitratedehydrogenase EC:1.1.1.44 6-phosphogluconatedehydrogenase EC:1.1.1.49 glucose-6-phosphate1-dehydrogenase EC:1.11.1.9 glutathioneperoxidase EC:2.5.1.16 spermidinesynthase EC:6.3.1.8 glutathionesynthase

Note: Though this code works, but it just to show the concept am describing. On a 'good' day since the two while loops "smell" the same one can 'string' them together in a way to avoid DRY
Hope this helps.
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

In reply to Re: comparing 2 files and creating third file with uncommon content by 2teez
in thread comparing 2 files and creating third file with uncommon content by rashichauhan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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: (6)
As of 2024-04-24 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found