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

if statement

by Anonymous Monk
on May 08, 2002 at 04:56 UTC ( #164907=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I am trying to read a file and if it has the word unclassfied in it , to send to the good file and if not send to the bad file. any suggestions.
#!/usr/bin/perl use warnings; use strict; #open 3 filehandles open Classified, "c:/begperl/classified1_sports.dat" or die "Error mes +sage here: $!\n"; open Good, ">c:/begperl/good.dat" or die "Error message here: $!\n"; open Bad, ">c:/begperl/bad.dat" or die "Error message here: $!\n"; while (<Classified>){ if (Classified == unclassified) print FH Good\n; else { print "bad\n"; }

Replies are listed 'Best First'.
Re: if statement
by tachyon (Chancellor) on May 08, 2002 at 05:55 UTC

    Full marks for using warnings, strict, checking the results of opening your files and posting what you have so far. Assuming that you want to check each line in classified1_sports.dat for the string "unclassified" in lower case then print to good.dat if it is there or bad.dat if it is not then a few minor changes to your code and you are rocking:

    #!/usr/bin/perl use warnings; use strict; # open 3 filehandles open Classified, "c:/begperl/classified1_sports.dat" or die "Error mes +sage here: $!\n"; open Good, ">c:/begperl/good.dat" or die "Error message here: $!\n"; open Bad, ">c:/begperl/bad.dat" or die "Error message here: $!\n"; # read the classified1_sprots.dat file a line at a time while (<Classified>){ my $line = $_; # we get the line in $_ if ( $line =~ m/unclassified/ ) { # use a match regex to check for + string print Good $line; # print $line to filehandle Good } else { print Bad $line; # print $line to filehandle Bad } } # close our files when finished with them close Classified; close Good; close Bad;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: if statement
by IndyZ (Friar) on May 08, 2002 at 05:09 UTC
    From the level of code you presented here, I would recommend a book like "Learning Perl" from O'Reilly. It will cover much of the general programming experience that you seem to be lacking as well as all of the other concepts that you need to complete this task.

    What follows are a few of the errors in your code.

    • Why are you opening "Bad"? You never write to it.
    • The while-loop will run forever. - see tachyon's reply.
    • "Classified == unclassified" won't work since you are comparing a filehandle to something that doesn't exist.
    • "print FH Good\n;" - You never opened the filehandle FH.
    • Make sure you close() any files you open

    update: Moved recommendations to the top.

    --
    IndyZ

      The while-loop will run forever

      Actually this is a very common idiom for reading a file a line at a time.

      while(<DATA>) { print; } __DATA__ The while loop will finish now!

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Whoops. I was looking at a copy that I had pasted into my node, sans <code> tags so it disappeared on me.

        --
        IndyZ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-09-27 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (119 votes). Check out past polls.

    Notices?