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

I am attempting to open up a file in the following format: The first word is the item to search for and the second is the new search term I want printed. EX: skin -new skin The second thing I want is to open up the file that is to be searched. If the search item above appears anywhere in the file then I want the new search term printed to another file. Can anyone tell me why this regular expression is not matching?  if  ($line =~ /$term)/i) { Below is the entire code:

my $dir = 'c:/perl_scripts/testing'; my $fsearch = "$dir/input.txt"; my $ssearch = "$dir/isearch.txt"; my $countsection = 1; my $count = 0; my $line; # Opens file with search items open (OFILE,"<$ssearch")||die "Could not open input file isearch.txt\n +"; # Opens file with list of files to be searched open (INFILE,"<$fsearch")||die "Could not open input file input.txt\n" +; while($sitem = <OFILE>) { chomp $sitem; @newterm = split (/\-/, $sitem); $term = $newterm[0]; while($sigfile = <INFILE>){ chomp $sigfile; open (TEXTFILE, "<$dir/$sigfile") || die "\nCAN'T OPEN $fsearc +h\n"; #Create NEW File to output Revised Data open(COMBINE_FILE, ">>$dir/REVISE_$sigfile")|| die "\nCAN'T OPEN R +EVISE_$sigfile\n"; while($line = <TEXTFILE>) { chomp $line; my @sepdata = split (/\s+/, $line); if($sepdata[0] eq "+") { $countsection++; } #Prints out top of file to a new external file with no modifications a +nd adds new "pool term" to the end of section 2 if ($countsection <= 3){ print COMBINE_FILE "$line\n"; } if ($line =~ /$term)/i) { if ($countsection > 3 && $sepdata[0] eq "+" ) { print COMBINE_FILE "$newterm[1]\n"; } } if ($countsection > 3) { printf COMBINE_FILE "%-3s", $sepdata[0]; printf COMBINE_FILE "%3s ", $sepdata[1]; printf COMBINE_FILE "%-10s", $sepdata[2]; printf COMBINE_FILE "%4s", $sepdata[3]; printf COMBINE_FILE "%5s ", $sepdata[4]; printf COMBINE_FILE "%3s ", $sepdata[5]; printf COMBINE_FILE "$sepdata[6]\n"; } } } }

READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: trouble matching
by ghenry (Vicar) on Jul 25, 2005 at 15:02 UTC

    Try adding:
    use strict;
    use warnings;

    And see if that sheds any light.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      And indent meaningfully.
Re: trouble matching
by sk (Curate) on Jul 25, 2005 at 16:38 UTC
    I have not looked at your code carefully but this is what i understand from your description. You have bunch of keywords to be replaced (stored in one file). Then you check for them in another file (2nd file) and write it out to a 3rd file. The code below should get you started. I have not taken care of all the details but it should give you enough idea on how to make changes to the code -

    NOTE: Code not tested

    #!/usr/bin/perl -w my %lookup = (); # Construct the lookup table while (<INFO>) { chomp; my @list = split; # assuming space separated $lookup{$list[0]} = $list[1]; # search => replace } # Go through the search file and check for lookup strings using # the hash and replace it with the hash-value while (<SRCHFILE>) { for my $k (keys %lookup) { s/$k/$lookup{$k}/g; } print; }

    Picking a keyword from a file and then searching for it in the other file and re-reading them it very inefficient. If the first file is manageable i would use a hash as in above example

Re: trouble matching
by GrandFather (Saint) on Jul 25, 2005 at 18:08 UTC

    Examine the contents of $line and $term with a debugger, or print their contents like this:

    print "line >$line<, term >$term<\n";

    Perl is Huffman encoded by design.
Re: trouble matching
by graff (Chancellor) on Jul 26, 2005 at 02:50 UTC
    As pointed out in sk's reply, you should not be nesting your while loops. And you certainly do not need more than two while loops.

    You seem to have an idea already that some files contain "configuration" or "instruction" data (list of patter/replace pairs, list of text files to operate on), so read each of those files into a hash or array, as appropriate. Then figure out how to actually do all the work in a single pass over the data files. Maybe something like this:

    use strict; use warnings; # ... initialize some path and file names ... # Open file with search items my %terms; open(FILE,"<$ssearch") or die "$ssearch: $!"; while (<FILE>) { # use $_ chomp; my ( $term, $newterm ) = split /-/; $terms{$term} = $newterm; } # Open file with list of files to be searched open(FILE,"<$fsearch") or die "$fsearch: $!"; my @sigfiles = <FILE>; chomp @sigfiles; # loop over the files to be searched for my $file ( @sigfiles ) { open( IFILE, $file ) or die "$file: $!"; open( OFILE, ">$dir/REVISE_$file" or die "$dir/REVISE_$file: $!"; # loop over lines in the file while (<FILE>) { ## at this point, it's hard to tell what you really want to do... ## you can refer to "keys %terms" and "values %terms" and "$terms{$k +ey}" ... } ## do you need to print something to your output that serves as ## some sort of "summary" for each input file? If so, do that here. ## (you probably should simplify/combine your various printf statemen +ts) }
    Good luck with that...