in reply to Re: Match a list of numbers from a list of files
in thread Match a list of numbers from a list of files

I am not sure I am loading the numberFH correctly. I need to get that from a file and also the transaction files. Here is my code, not sure if this is right.
open IN1,"cat $DATA_DIR/$INFILE_NAME |" or die "Can't open $INFILE_NAM +E: $!\n"; while ($line1 = <IN1>) { chomp(my @numbers = $line1); } close(IN1); my $rxFindTrans = do{ local $" = q{|}; qr{(@numbers)}; }; open IN2,"cat $TransLogs/$FILE_NAME |" or die "Can't open $FILE_NAME: +$!\n"; local $/ = qq{</DU>\n}; while( <IN2> ) { s{.*(?=<DU>)}{}s; next unless m{$rxFindTrans}; print qq{Found $1 in:\n$_}; print qq{==================\n}; } close(IN2);

Replies are listed 'Best First'.
Re^3: Match a list of numbers from a list of files
by johngg (Canon) on Dec 19, 2008 at 10:09 UTC

    There's no need to pipe cat into your filehandles as you can open files directly; the three-argument form with lexical filehandles is recommended practice.

    Instead of

    open IN1,"cat $DATA_DIR/$INFILE_NAME |" or die "Can't open $INFILE_NAM +E: $!\n";

    do

    open my $in1FH, q{<}, $DATA_DIR/$INFILE_NAME or die qq{Can't open $INFILE_NAME: $!\n};

    As you suspected, you are not reading the numbers file correctly. From your original post it looks like you have 5000 or so numbers in a file, one per line. If you assign the readline into an array rather than a scalar then the whole file is read into the array, one line per element. Furthermore, chomping an array will remove the line terminator from every element in the array. You could read the file line by line in a while loop instead if you like but then you would have to push each line onto the array. These two bits of code are equivalent.

    Using a loop

    my @numbers = (); while( <$in1FH> ) { chomp; push @numbers, $_; }

    Reading directly into an array.

    chomp( my @numbers = <$in1FH> );

    You have removed the bare code block around the reading of the second file. It was there so that the local $/ ... really was localised to that scope to avoid possible side effects later in your script. Since you have a lot of files to read you could perhaps do something like

    my @filesToRead = ( populate this list somehow ); ... foreach my $file ( @filesToRead ) { open my $in2FH, q{<}, $file or die qq{Can't open $file: $!\n}; local $/ = qq{</DU>\n}; while( <$in2FH> ) { ... } close $in2FH or die qq{Can't close $file: $!\n}; }

    I hope this is helpful.

    Cheers,

    JohnGG

      This worked. Thanks again for all your help. This script is saving me a lot of time.