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

Btrott, Thanks a lot for your help. I still have a little problem I modified the program a little bit. It will be recieving information from a file. The program is supposed to parse the line with total in it. there is file rows there. i need to use the las two, the one with the total.
..... ..... .... .... .... ..... ..... .... .... .... total 1234 1234 5678 7894
I need to use the the numbers "5678" and "7894", and graph them in excel everyday. The graph would be "day" vs "total". I hope this makes what I am trying to do a little easier to understand. Now my code reads...
#!/usr/local/bin/perl -w my($currentLine, $name, $infilename); chomp ($infilename = "temp1.pl"); if ($ currentLine =~ /^ \$name:/) { print $currentLine; }
Thanks alot, Paav

Replies are listed 'Best First'.
RE: RE: Re: required explicit package error.
by verbal (Initiate) on Jun 15, 2000 at 08:08 UTC
    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>
      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
      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

      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
RE: RE: Re: required explicit package error.
by btrott (Parson) on Jun 15, 2000 at 00:56 UTC
    I don't know what you're doing there. It seems you've solved your initial problem, but that code isn't going to work correctly. Here's some code that may do what you want:
    #!/usr/local/bin/perl -w use strict; while (<>) { next if !/^total/; my @fields = split /\s+/; my($day, $total) = @fields[3,4]; ## Now graph the values or do whatever you ## want with them. ... ## This will exit the loop after it's seen ## the first "total" line. If you don't want ## to do this (if, for example, you'll have ## several "total" lines, each of which you ## want to graph), take out this line. last; }
    To be invoked as such:
    % foo.pl <filename>