Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

hi, here is the code i create using suggestion by moritz.

1. put file2 into hash

2. processing file1 line by line, mapping with the config files.

and then search line by line by comparing 1st column of file1 and 2nd column of file2.

please advice whether i can optimize the code as the 1st files contain huge records.

#!/usr/bin/perl use strict; use warnings; my $sum = 0; #open(my $datamap, '<', "mapping.txt") or die "Could not open '$file' +$!\n"; open(my $data2, '<', "file2.txt") or die "Could not open '$file' $!\n" +; while (my $line = <$data2>) { chomp $line; my @fields = split "^", $line, -1; my $col2Val = $fields[1]; my $col1Val = $fields[0]; my $col3Val = $fields[2]; my $col4Val = $fields[3]; if (! defined $firstHashRef->{$col2Val}) { $firstHashRef->{$col2Val}->{Col1} = $col1Val; $firstHashRef->{$col2Val}->{Col3} = $col3Val; $firstHashRef->{$col2Val}->{Col4} = $col4Val; } } close($data2); open(my $data1, '<', "file1.txt") or die "Could not open '$file' $!\n" +; while (my $line = <$data2>) { chomp $line; my @fields = split ",", $line, -1; my $col1= $fields[1]; my $col4= $fields[3]; my $col6= $fields[5]; my $genmapping="grep '$col4\t$col6' $datamap"; my $mapping=`$genmapping`; if($mapping) { my $mapfound=split "\t", $mapping -1; if ((defined $firstHashRef->{$col1}) && ($mapfound[2]==$firstH +ashRef->{$col1}->{Col1}) ) { $fields[3]=$mapfound[0]; $fields[5]=$mapfound[4]; $fields[8]=$firstHashRef->{$col1}->{Col3}; $fields[9]=$firstHashRef->{$col1}->{Col4}; print OUT join( ',', @fields ) . "\n" ; } else { warn "We did not locate entry in hash table \n"; } } else{ $fields[3] = "unlim"; print join( ',', @fields ) . "\n" ; } } close($data1);

In reply to Re^2: file merge with config(parameter) files by hyans.milis
in thread file merge with config(parameter) files by hyans.milis

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2014-12-26 05:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (165 votes), past polls