Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to RE: RE: required explicit package error. by nuance
in thread required explicit package error. by Paav

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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









    Results (94 votes). Check out past polls.

    Notices?