In the first file, I basically store the data in a hash of array structure where I used 'Iteration...' as my key. When I loop through the second file, I would simply check if the key exists and if it does, then it should print the corresponding data.
#!/usr/bin/perl use strict; my $file1 = <<END_FILE2; Iteration 1: 0: Data: 1023 1023 1: Data: 200 1023 2: Data: 300 1023 3: Data: 250 1023 4: Data: 1023 1023 Iteration 2: 0: Data: 1023 1024 1: Data: 1023 60 2: Data: 1023 90 3: Data: 1023 100 4: Data: 1023 1023 END_FILE2 my (%record, $key); for my $i (\$file1) { open( my $file_1, '<', $i ) or die "cannot open file"; while( <$file_1> ) { $key = $1 if ( s/^(Iteration \d\d?):// ); push( @{ $record{$key} }, $1 ) if ( /(\d:\s.*)/ ); } close( $file_1 ); } while (my $line = <DATA>) { if ( $line =~ s/^(Iteration \d\d?):// ) { $key = $1; print "$key:\n"; } my ($index) = $line =~ /^(\d)/; print "File1:$record{$key}->[$index] File2:$line" if ( defined( $index ) ); print "\n" if ( $index == $#{ $record{$key} } ); } __DATA__ Iteration 1: 0: Data: 1023 1023 1: Data: 250 1023 2: Data: 60 1023 3: Data: 99 1023 4: Data: 1023 1023 Iteration 2: 0: Data: 1023 1024 1: Data: 1023 60 2: Data: 1023 90 3: Data: 1023 100 4: Data: 1023 1023

In reply to Re: How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files by bichonfrise74
in thread How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.