in reply to Find partially matching line in file - print that line to another file

Well, you didn't actually say _how_ it wasn't working as expected. But I'll take a stab and assume that the last line in file two is being printed to your result set, when you don't want it to be.

But regardless, this code:

#!/usr/bin/perl -l use strict; use warnings; my @fileone = qw/123456 123457 123458/; my @filetwo = ("123456 foo", "123456 bar", "123457 foobar", "123455 this wouldn't be printed to new file" ); foreach my $pattern (@fileone) { foreach my $line (@filetwo) { if ($line =~ /^$pattern/) { print $line; } } }
Gives this result:
123456 foo 123456 bar 123457 foobar
Which is as expected. You probably need to show us more of your code. I suspect that you're probably missing a chomp somewhere along the line...

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^2: Find partially matching line in file - print that line to another file
by rkmase (Novice) on Dec 14, 2007 at 15:14 UTC
    Thanks Darren, Your results are what I would like to get, but I actually get a blank file instead. I'll keep poking around then - may be a formatting issue somewhere.
      Like i said, if you show us some more code - at least a self-contained example that can be run - that demonstrates the problem, I'm sure you'll get the answer very quickly.

      Cheers,
      Darren :)