Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

RE: required explicit package error.

by Paav (Novice)
on Jun 15, 2000 at 21:20 UTC ( #18328=note: print w/replies, xml ) Need Help??


in reply to RE: RE: Re: required explicit package error.
in thread required explicit package error.

Verbal, I tried the code out the way you had asked.It takes the input in however, it doesn't do anything with it. By the way, I am the one, who is trying to read the total from a file, and spit it out to another file. My code reads following:
print "Input file name: "; $_ = <>; chop; open(INFILE, $_); while (<INFILE>) { if(m/total/) { s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/; print; } } close INFILE
I think the program is getting the input, however it is just not doing anything with it. Once I enter the name of the file, it just sits there, instead of parsing. How come?? THanks Paav

Replies are listed 'Best First'.
RE: RE: required explicit package error.
by nuance (Hermit) on Jun 15, 2000 at 23:43 UTC

    You never actually print to anywhere, try opening another file for output. I've rewritten your example above, call this with

    progname.pl datafile results

    #!/usr/bin/perl -w use strict; my($in, $out, @rest) = @ARGV; open (INFILE, "<$in") or die "can't open $in $!"; open (OUTFILE,">$out") or die "can't open $out $!"; while (<INFILE>) { print OUTFILE "$1 $2\n" if /^total.*(\d+)\s+(\d+)\s*$/; } close INFILE; close OUTFILE;

    Notice you can use the same regular expression to match the line and extract the data, also the regular expression is more general and will catch lines that aren't formatted exactly as you anticipated.

    Update: I should probably explain the regular expression, just in case you aren't totally familiar with them. It matches any line that begins with total, has anything at all in the middle, but ends with tw0 numbers separated by whitespace. It also has optional whitespace at the end (spaces at the ends of lines can be a real pain). Note the use of + for items that definitely have to be there and * for things taht are optional.

    Nuance

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2022-06-30 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?