Re: Matching a Scalar Variable





Charles K. Clarkson wrote:

JupiterHost.Net <mailto:mlists@xxxxxxxxxxxxxxx> wrote:

: This will open both files and check each line of file2 against
: each line of file1 (and it alerts you to any problems, is easier
: to read, best pratice safe (hopefully ;p), ect etc):

    Sorry, but it won't. It will open both files and check the
first line of file1 against all the lines of file2. All the rest
of the lines in file1 are not tested. The inner loop only runs
once. The pointer into file2 is never set back to the file start.

Good point :)

Perhaps I should have worded it "this does the same thgin you were doing in your example" :)

If file1 has:
1
2
3
4
and file2 has:
a
b
c
d
then the output of:

#!/usr/bin/perl

use strict;
use warnings;

open my $file1_fh, '<', 'file1' or die "file1 open failed: $!";
open my $file2_fh, '<', 'file2' or die "file2 open failed: $!";

while(<$file1_fh>) {
    my $line = $_;
    chomp $line;
print "File 1 $line\n";
    while(<$file2_fh>) {
        my $match_against = $_;
        chomp $match_against;
print "\tFile 2 $match_against\n";
        # print "Got the string\n" if $line =~ m{$match_against}xms;
    }
}

close $file1_fh;
close $file2_fh;

is:
File 1 1
        File 2 a
        File 2 b
        File 2 c
        File 2 d
File 1 2
File 1 3
File 1 4


But if you do it liek so:


#!/usr/bin/perl

use strict;
use warnings;

open my $file1_fh, '<', 'file1' or die "file1 open failed: $!";
open my $file2_fh, '<', 'file2' or die "file2 open failed: $!";

my @match_list = <$file2_fh>;
close $file2_fh;

while(<$file1_fh>) {
    my $line = $_;
    chomp $line;
print "File 1 $line\n";
    for my $match_against (@match_list) {
        chomp $match_against;
print "\tFile 2 $match_against\n";
        # print "Got the string\n" if $line =~ m{$match_against}xms;
    }
}

close $file1_fh;

then the output is:

File 1 1
        File 2 a
        File 2 b
        File 2 c
        File 2 d
File 1 2
        File 2 a
        File 2 b
        File 2 c
        File 2 d
File 1 3
        File 2 a
        File 2 b
        File 2 c
        File 2 d
File 1 4
        File 2 a
        File 2 b
        File 2 c
        File 2 d

Thanks for catching my misstatement :)

So this will do what I originally said (IE instead of copying the original example I commented on):

#!/usr/bin/perl

use strict;
use warnings;

open my $file1_fh, '<', 'file1' or die "file1 open failed: $!";

open my $file2_fh, '<', 'file2' or die "file2 open failed: $!";
my @match_list = <$file2_fh>;
close $file2_fh;

while(<$file1_fh>) {
    my $line = $_;
    chomp $line;

    for my $match_against (@match_list) {
        chomp $match_against;
        print "Got the string\n" if $line =~ m{$match_against}xms;
    }
}

close $file1_fh;

.



Relevant Pages

  • Re: Shell script: Read and edit files
    ... close $file1; ... Glenn Jackman ...
    (comp.unix.shell)
  • Re: simple ln question
    ... that makes it so the contents of file1 are the same as of file2, and if you change the contents of 1, the other ... prints the inode number for the file foobar. ...
    (Fedora)
  • RE: data conundrum
    ... in your spens you'll want to keep field names consistent. ... the specs will be the most tedious, but once theyr're done once they're done. ... in other words, you have file1, file2, file3..... ...
    (microsoft.public.access.externaldata)
  • RE: data conundrum
    ... let's say file1 looks like this: ... and let's say file2 looks like this: ... then you will probably have to create import specs for all 100 ... create from the .csv files) as opposed to using a form, ...
    (microsoft.public.access.externaldata)
  • Re: Need Help
    ... eg: file1, file2, file3 etc ... into, max 5-6, chunk files and then loading these chunk files into ...
    (comp.unix.shell)