Ok, I asked this earlier in the CB, but I think it was to hard to explain there, so im posting this nice question for your viewing pleasure. :) The main reason I post this question to you is not for sheer enjoyment, But to find other ways to do the same thing.

Anyway, let me start off by posting example code:

And the code:
use warnings; use strict; my $f1 = 'file1.txt'; my $f2 = 'file2.txt'; my @original; my @gottem; open( my $file1, '<', $f1 ); while ( my $line1 = (<$file1>) ) { next if ( $line1 =~ /^#/ ); chomp($line1); open( my $file2, '<', $f2 ); while ( my $line2 = (<$file2>) ) { next if ( $line2 =~ /^#/ ); chomp($line2); my ( $leftside, $rightside ) = split( /\s/, $line2, 2 ); if ( $line1 =~ $leftside ) { push( @original, $rightside ); } } } # print "$_\n" for(@original); for (@original) { open( my $file_2, '<', $f2 ); while ( my $line_2 = (<$file_2>) ) { my ( $first, $last ) = split( /\s/, $line_2, 2 ); if ( $last =~ $_ ) { push( @gottem, $line_2 ); } } } print $_ for (@gottem);
here is some sample data:

file1.txt
123 456 789
file2.txt
123 string 1 111 string 1 script should skip this line 222 string 1 333 string 1 456 string 2 444 string 2 it should skip this line as well 555 string 2 666 string 2 789 string 3 777 string 3 also skipping this line too 888 string 3 999 string 3
Output:
123 string 1 111 string 1 222 string 1 333 string 1 456 string 2 444 string 2 555 string 2 666 string 2 789 string 3 777 string 3 888 string 3 999 string 3
Thanks for the tips btw :)

What I am trying to do is take each string from file1.txt, and match it in file2.txt which populates @original. Once that is done I loop through @original and compare it back against file2.txt but this time I am comparing the right side of the split to the current line in file2.txt Then a final push to @gottem, just incase I need to use this list somewhere else.

This will allow me to catch all lines that match with 'string 1' or 'string 2' in file2.txt, because those are what I am going for, but there is no way to get the whole string out of file2.txt without some kind of moderate algorithm.

Like I said, I really would love to see some other ways to do this.

If you have any questions on need me to try to clarify/explain it more, just let me know. EDIT: It seems there is indeed an error in the output, there are some additional 'string 2's in there after 'string 5'. I am not sure how they got in there but hopefully you still see my idea, and can expand the horizon :D

I will tinker with it a little more and try to get the correct output, sorry for any confusion.

EDIT: Updated the post with sample data and output is expected.


In reply to UPDATED - Getting data from second file, based on first files contents; by james28909

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.