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

I have some code that I'm creating and it all works perfectly except for one part. Near the bottom I try to compare if one string matches another from a while loop. If I put in a static string it executes whats inside of the if statement otherwise it doesn't. A note is I didn't list the hash I use in the code but it does work fully.
# Open the inventory file open(HANDLE,"<INVENT.TXT"); while(<HANDLE>) { # Separate each field for a single row @info = split(/,/,"$_"); $code = $info[0]; $description = $info[2]; $manuCode = $info[3]; $retailPrice = $info[9]; $invCount = $info[30]; # Strips out useless characters $code = substr($code,1,length($code)-2); $description = substr($description,1,length($description)-2); $manuCode = substr($manuCode,1,length($manuCode)-2); $retailPrice = substr($retailPrice,1,length($retailPrice)-3); $invCount = substr($invCount,1,length($invCount)-5); # Strips out whitespaces when needed and multiple zeros $code =~ s/\ {1,}//g; $description =~ s/\ {2,}//g; $retailPrice =~ s/0{2,}//g; $invCount =~ s/0{2,}//g; # Converts inventory count to 0 if there is no inventory if($invCount == '') { $invCount = 0; } # Compare the manufacturer code and change it if needed while(($key,$value) = each(%catReplace)) { if($manuCode =~ /$key/) { $manuCode = $value; last; } } # Open the manufacturer file open(MAN,"<file.csv"); while(<MAN>) { # Separate each field for a single row @manInfo = split(/,/,"$_"); $manufacturer = $manInfo[0]; $manuID = $manInfo[1]; # THIS IS WHERE I HAVE PROBLEMS if($manuCode =~ /$manuID/) { $manuCode = $manufacturer; last; } } close(MAN); # Open and write the new inventory list open(NEWFILE,">>test.txt"); print NEWFILE $code.',',$description.',',$manuCode.',',$retailPric +e.',',$invCount."\n"; close(NEWFILE); } close(HANDLE);
Any help would be appreciated.

Replies are listed 'Best First'.
Re: Regex problem
by kennethk (Abbot) on Dec 15, 2008 at 20:15 UTC

    If you are actually testing equivalence, it could be both faster and not prone interpolation problems to if you use eq instead of a regex. This also works for substrings if you know placement within the test string.

      I just tried that and it still doesn't work. Another note is that I have tested what the code outputs for both the $manuCode and $manuID. For each code the id looks identical. What it seems like I'm getting is ASD=ASD for example.
        I threw your code into my editor and threw in use strict. Most looks fine, but the %catReplace hash doesn't get initialized anywhere I can see - would this matter? The other obvious possibility is that you've got whitespace problems. Have you tried seeing if length returns the expected values?
Re: Regex problem
by ccn (Vicar) on Dec 15, 2008 at 20:08 UTC

    You may try to use \Q metacharacter to disable other metacharacters which can occure in $manuID variable and affect to matching

    if ( $manuCode =~ /\Q$manuID/ ) {

    See perldoc perlre

      I had tried that before and I tried it again and it doesn't help, the codes are only 3 alphanumeric characters. Also i noticed in my code I forgot to add a line. I did a substitute to remove newlines and tabs from the variables with the csv.