Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Help with Search String

by btobin0 (Acolyte)
on Dec 07, 2007 at 11:59 UTC ( [id://655626]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Help with Search String
in thread Help with Search String

Now I have edited a few things and it now prints everything in the list. It print the output with the right response. but when saved it prints all 4 sentences. What am I doing wrong?
use strict; use warnings; my $namesFile = q{names.txt}; open my $namesFH, q{<}, $namesFile or die qq{open: $namesFile: $!\n}; my @names = <$namesFH>; close $namesFH or die qq{close: $namesFile: $!\n}; chomp @names; # Remove line terminators my $namesPatt = join q{|}, @names; my $dataFile = q{data2.txt}; open my $dataFH, q{<}, $dataFile or die qq{open: $dataFile: $!\n}; while ( <$dataFH> ) { print qq{Found name $_\n} if /($namesPatt)/; open FILE, ">>/home/btobin/data3.txt" or die "Unable to Open: $!"; print FILE "$_\n"; <--- somehow this is wrong close FILE; } close $dataFH or die qq{close: $dataFile: $!\n};
Sentences used:
This is a data file created to learn how to use Perl . This is how Bob found his way. I wonder what Jonny is going to do. This is a test.

Replies are listed 'Best First'.
Re^5: Help with Search String
by johngg (Canon) on Dec 07, 2007 at 12:42 UTC
    Your error is that you do print FILE "$_\n"; unconditionally so that every line of your input file is printed to your output file. There are other problems with your code.

    • Do you really want to append your results to data3.txt rather than overwriting the file each run?

    • Whether you append or overwrite, open and close the file outside of the while loop.

    • Test for the success of your close operation.

    • It is recommended to use lexical rather than bare word filehandles and the three-argument form of open.

    The code becomes

    my $outFile = q{/home/btobin/data3.txt}; open my $outFH, q{>>}, $outFile or die qq{open: $outFile: $!\n}; while ( <$dataFH> ) { print $outFH qq{Found name $_\n} if /($namesPatt)/; } close $outFH or die qq{close: $outFile: $!\n};

    I hope this is helpful.

    Cheers,

    JohnGG

      Thanks you for your help. This works great. Overwrite would be better. To change that I would need to change the line:
      open my $outFH, q{>>}, $outFile
      Correct?
      i have a question I have put this together and it works great in Solaris, but in Linux the search results show everything. Got any ideas?
      use strict; use warnings; my $namesFile = q{names.txt}; open my $namesFH, q{<}, $namesFile or die qq{open: $namesFile: $!\n}; my @names = <$namesFH>; close $namesFH or die qq{close: $namesFile: $!\n}; chomp @names; # Remove line terminators my $namesPatt = join q{|}, @names; my $dataFile = q{data2.txt}; open my $dataFH, q{<}, $dataFile or die qq{open: $dataFile: $!\n}; # while ( <$dataFH> ) # { # print qq{Found name $_\n} if /($namesPatt)/; my $outFile = q{/home/btobin/data3.txt}; open my $outFH, q{>}, $outFile or die qq{open: $outFile: $!\n}; while ( <$dataFH> ) { print $outFH qq{Found name $_\n} if /($namesPatt)/; } close $outFH or die qq{close: $outFile: $!\n}; # } close $dataFH or die qq{close: $dataFile: $!\n};
        To answer your previous post, yes, change q{>>} to q{>} as you have already discovered :-)

        Regarding Solaris versus Linux, I work mostly on Solaris but occasionally port my scripts to Linux. I have never had problems when doing that so I don't really know what might be going wrong. Check the obvious like making sure you have the same data files and script on both systems. After that it is probably a case of scattering extra print statements through you script to check that the data you think you are working with is in reality what you expected.

        Sorry I can't be of more help.

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://655626]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found