Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

print failing to output anything

by Anonymous Monk
on Nov 12, 2003 at 17:59 UTC ( [id://306571]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there, I am at a loss at why the following bit of code won't give me any output, its meant to extract POS-tags:
#!/usr/bin/perl #use strict; my $filename = ""; my $outputfile = ""; print "Input file name:"; $filename = <STDIN>; chop ($filename); open (INFILE, "$filename") || die "error opening $filename $!\n"; print "Output file name:"; chop($outputfile = <STDIN>); open (OUTFILE, ">$outputfile") || die "error opening $outputfile $!\n" +; while (<INFILE>) #read the input file { while ( /(\w['\w-]*)/g ){ #if we have a "word"; } my $word = ""; my $filename = ""; my $outputfile = ""; my $ending = "/NN"; my $plending = "/NNS"; my $properending = "/NNP"; my $nounphrase = ""; foreach $word (( /(\w['\w-]*)/g )){ if($word =~ /$ending$/i){ print OUTFILE "word = $word, is a plural Noun\n"; #$word =~ s/NN$//i;# chop the ending #print OUTFILE "$word\n"; }#end of 1st if } foreach $word (( /(\w['\w-]*)/g )){ if($word =~ /$plending$/i){ print OUTFILE"word = $word, is a Noun\n"; #$word =~ s/NN$//i;# chop the ending #print OUTFILE "$word\n"; }#end of 1st if } foreach $word (( /(\w['\w-]*)/g )){ if($word =~ /$properending$/i){ print OUTFILE"word = $word, is a Proper Noun\n"; #$word =~ s/NN$//i;# chop the ending #print OUTFILE "$word\n"; }#end of 1st if } close OUTFILE; }
can you help???

Edit by castaway - added code tags, changed title from 'OUTPUT'

Replies are listed 'Best First'.
Re: print failing to output anything
by Roy Johnson (Monsignor) on Nov 12, 2003 at 20:57 UTC
    Some comments:
    my $filename = ""; my $outputfile = "";
    No real need to declare and initialize these here. You can do it when you get the input:
    my $filename = <STDIN>; chop ($filename);
    Should use chomp, instead.

    Then you make the computer read the whole file and do a bunch of matches that you completely ignore:

    while (<INFILE>) #read the input file { while ( /(\w['\w-]*)/g ){ #if we have a "word"; }

    Then a whole bunch of (re-)declarations and initializations, several of which were pointless, then some attempts to match on $_, but none of them happen because it's empty now.

    The main problem is that you haven't wrapped your file-reading loop around the code that's supposed to be doing the work. Apart from that, you've made some sub-optimal decisions about what to use: the foreach's should probably be whiles, and the patterns should be qr//'d instead of double-quoted.

Re: print failing to output anything
by Zed_Lopez (Chaplain) on Nov 12, 2003 at 20:04 UTC

    The problem isn't with print, it's with the rest of your code.

    The empty while loop near the top does nothing.

    Consider using the \b word boundary assertion to do your splitting (or just use split.)

    If you want to iterate over a list of words with m/.../g, use a while loop. Your foreach loops are assigning a list to a scalar ($word), thus the scalar's value during every iteration of the loop is that of the last element of the list (the last word on the line). Almost certainly not what you want.

Re: print failing to output anything
by castaway (Parson) on Nov 12, 2003 at 19:09 UTC
    Back to the task at hand...

    Can you show us some sample input to this? If its not outputting anything, then clearly the input you're using isnt matching your regular expressions.

    Also, 2 of those print statement lines are incorrect, you are missing a space between the 'OUTFILE' and the actual output, which won't work as you intend.

    You should probably add a 'use warnings;' to the top of this, which will catch that error, and possibly some others as well.

    C.

Re: print failing to output anything
by ysth (Canon) on Nov 12, 2003 at 22:39 UTC
    To other monks replying, the empty while loop is a red herring.

    Looks like you are looping through the file and checking for "words" ending with /NN, /NNP, or /NNS. But your definition of word /\w['\w-]*/ doesn't allow them to contain a / character, so nothing matches.

    A common problem with using //g is that it silently ignores bad input, leading to surprises.

Re: print failing to output anything
by jZed (Prior) on Nov 12, 2003 at 18:08 UTC
    Dude! If I could edit your node, I would. Put <code></code> tags around your script if you want anyone to be able to read it. update /me takes own advice and puts tags around tags
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found