Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

RE: RE: Re: required explicit package error.

by verbal (Initiate)
on Jun 15, 2000 at 08:08 UTC ( #18242=note: print w/ replies, xml ) Need Help??


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

It sounds like you just want a simple script to open a file, find the line that says "total", and report the last two rows of numbers. Here is some code to do this :

#!/usr/local/bin/perl 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;
There are probably 3 things in this script that are non-intuitive to the budding Perl programmer :

1) The use of $_
2) The use of <>
3) The use of regular expressions

Here they are, demystified :

1) $_ is the "default variable" when you don't supply any other variable name to a sub (such as chop and print in our example).

2) <> abbreviates <STDIN>

3) There are two regular expressions in this example, the first one is a matching regular expression (m/total/) which returns true if and only if the text "total" exists as a substring of $_

The second regular expression is a search and replace regex.
s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/
all it does is replace "^total\sA\sB\sC\sD$" with "C D" where ^ (caret) matches start of line, \s matches space, and A B C and D are numbers (1 or more digits). The use of the parenthesis following the first / provides Perl's regular expression engine the ability to do what is called "backreferencing" (using $1 and $2 after the second /).

Let me know if that code works for you, and if there's anything else you'd like to do.

-verbal </CODE>


Comment on RE: RE: Re: required explicit package error.
Select or Download Code
RE: RE: RE: Re: required explicit package error.
by Anonymous Monk on Jun 15, 2000 at 21:16 UTC
    Verbal, I tried your method but I still get an error. It takes the file in however, it does nothing with it. how come??
    #!/usr/local/bin/perl 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;
    Thanks a lot for yout help.. Paav
RE: required explicit package error.
by Paav (Novice) on Jun 15, 2000 at 21:20 UTC
    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

      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

i can input a file, but not parse it
by Paav (Novice) on Jun 15, 2000 at 21:29 UTC
    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 codes seems OK. THe name of the file that i will be reading the data from is called, "elements". My question is, I am not sure if the file is even being read, or parsed. How would I print out the parsed information?? Thanks Paav

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-10-02 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (65 votes), past polls