in reply to Question on file compare

First, you can shorten your code just a bit by removing the use of $_.

while (my $ptfkey = <F2>)

The regex where you read in file 2 has a couple of issues. First, it forces start and end of line matching (^ and $, respectively). Next, it only matches the pipe and the whitespace on either side of it, so it will only match lines with ' | \n'. Lastly, it deletes everything that it matches.

If you only need to identify information in the first column, you only need to do this:

while (<F2>) { $ptfkey = $_; # you can skip this using above suggestion $ptfpn = $ptfkey; # don't need this if just matching $ptfpn =~ m/^(\w+)/; # if only letters and numbers $ptfpn =~ m/^([\d\-\.]+)/; # if it's a float $F1hash{$1} = 1; }

Secondly, the line that checks for the value should be throwing you an error:

if ($PartNumber =~ $Flhash{$PartNumber}) {

(Or, there are some languages that treat '=~' as a negation, kind of like '!=', so it could just be a typo.)

All you need to do is check to see if the value exists in the hash we created above:

if (exists $F1hash{$PartNumber}) {

Hope this helps.

Edit: I also just realized you declared %F1hash twice -- you should add the following to your code:

#!c:\perl\bin\perl -w # note the addition of the -w use diagnostics; use strict;

Replies are listed 'Best First'.
Re^2: Question on file compare
by Marshall (Canon) on Mar 13, 2012 at 23:06 UTC
    The regex where you read in file 2 has a couple of issues. First, it forces start and end of line matching (^ and $, respectively). Next, it only matches the pipe and the whitespace on either side of it, so it will only match lines with ' | \n'. Lastly, it deletes everything that it matches.

    The pipe character, if un-escaped means "or" in a regex. This regex would delete the white space in a line containing only white space - not too useful. I don't see that /g does anything. Not sure what the OP wanted to happen. But you are right, this almost assuredly not it.