|Problems? Is your data what you think it is?|
RE: RE: Re: required explicit package error.by verbal (Initiate)
|on Jun 15, 2000 at 08:08 UTC||Need Help??|
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 :
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.
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.