rkmase has asked for the wisdom of the Perl Monks concerning the following question:

Code below has the two files read into arrays - while *I* think this should work, perl thinks differently. Can someone show me what I did wrong?

File one has:
123456
123457
123458

File two has:
123456 foo
123456 bar
123457 foobar
123455 this wouldn't be printed to new file

open(RESULT, "> result.txt") or die ("Unable to open file"); foreach $pattern(@fileone) { foreach $line(@filetwo) { if ($line =~ /^$pattern/) {print RESULT $line."\n";} } }
  • Comment on Find partially matching line in file - print that line to another file
  • Download Code

Replies are listed 'Best First'.
Re: Find partially matching line in file - print that line to another file
by glide (Pilgrim) on Dec 14, 2007 at 15:03 UTC
    Hi,

    my code it's a bit different, but it's working ... what is the problem?

    use strict; my @fileone = qw/ 123456 123457 123458/; while (my $line = <DATA>) { foreach my $pattern(@fileone) { if ($line =~ /^$pattern/) {print $line."\n";} } } __DATA__ 123456 foo 123456 bar 123457 foobar 123455 this wouldn't be printed to new file
    output
    123456 foo 123456 bar 123457 foobar
      This is exactly what I was looking for. I imagine it was the different order that I tried to process mine that did it.

      Thanks! Mason
Re: Find partially matching line in file - print that line to another file
by johngg (Canon) on Dec 14, 2007 at 16:00 UTC
    Rather than iterating over the lines of "file1" each time you test a line from "file2", construct a single regex that has all the lines from "file1" as alternations.

    use strict; use warnings; open my $file1FH, q{<}, \ <<END_OF_FILE1 or die qq{open: $!\n}; 123456 123457 123458 END_OF_FILE1 chomp( my @file1Lines = <$file1FH> ); close $file1FH or die qq{close: $!\n}; my $rxFile1 = do { local $" = q{|}; qr{^(?:@file1Lines)}; }; open my $file2FH, q{<}, \ <<END_OF_FILE2 or die qq{open: $!\n}; 123456 foo 123456 bar 123457 foobar 123455 this would not be printed to new file END_OF_FILE2 while ( <$file2FH> ) { next unless m{$rxFile1}; print; }

    Here's the output.

    123456 foo 123456 bar 123457 foobar

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Find partially matching line in file - print that line to another file
by McDarren (Abbot) on Dec 14, 2007 at 15:06 UTC
    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 :)

      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 :)

Re: Find partially matching line in file - print that line to another file
by poolpi (Hermit) on Dec 14, 2007 at 14:54 UTC
    <<123455 this wouldn't be printed to new file>>
    '123455' isn't in the first file.
    so $pattern never contains '123455' and the regexp does not match
    HTH,
    PooLpi
Re: Find partially matching line in file - print that line to another file
by moklevat (Priest) on Dec 14, 2007 at 15:05 UTC
    It's not entirely clear what output you are expecting from this. I was going to suggest a hash for your filetwo with the numeric portion being the keys and the text portion being the values, but you have duplicate entries for 123456. Are you looking for:

    foo bar foobar

    Update: Fortunately, others were able to get the gist. Clearly more caffeine is in order. I was wary of running through the entire pattern set of fileone for each line of filetwo without knowing how large your actual files are. Premature optimization...evil...etc.