Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

while loop, search & replace

by PJT (Initiate)
on Oct 01, 2004 at 17:47 UTC ( #395669=perlquestion: print w/ replies, xml ) Need Help??
PJT has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm trying to churn out my first perl prog and it's not going well. I have a log file from which I would like to extract 15 lines - I know there is exactly only 15 that will match the "if" case statement. When I extract them, I also want to insert a variable that contains the line number. For example:
call started 00:00:00, call ended 00:00:00, total modem active time 00 +:00:00

should look like:
Modem Line 0/$i : total modem active time 00:00:00

where $i is the variable that increase (only up to 15). When I run my program it spits out fifteen 1's and that's all. Any help is appreciated - PJT
#!/usr/sbin/perl -w # Program for modem pool log manipulation open(IN, $ARGV[0]) || die "cannot open $ARGV[0] for reading: $!"; open(OUT, ">$ARGV[1]") || die "cannot create $ARGV[1]: $!"; $i=0; while(<IN>){ if(/^call started/){ print OUT s/^call.*total/Modem Line 0\/$i : / ."\n"; $i++; } } close(IN); close(OUT);

Comment on while loop, search & replace
Select or Download Code
Re: while loop, search & replace
by ikegami (Pope) on Oct 01, 2004 at 17:54 UTC

    The '1' being printed the return value of s///, which (in scalar context) is the number of substitutions performed (1, in this case). You're also unintentionally removing the word 'total'. Solution:

    if(/^call started/){ s/^call.*total/Modem Line 0\/$i : total/; print OUT "$_\n"; $i++; }
      Thank you for your help. I'm still learning what "scalar" means. That solved my problem though. Cheers. PJT
        $a = something; # something is executed in a scalar context. @a = something; # something is executed in a list context. something; # something is executed in a void context. # Arrays return their number of elements in a scalar context: @b = qw( a b c ); print( @b , "\n"); # abc print(scalar(@b), "\n"); # 3 # print accepts a list, but scalar() forced scalar context. # Arithmetic forces scalar context: print(@b,"\n"); # abc print(@b."\n"); # 3 # Not just string arithmetic: print(@b, "\n"); # abc print(@b+0, "\n"); # 3 # Functions can examine their context: { local $, = ", "; local $\ = "\n"; print( localtime ); # 59, 14, 15, 1, 9, 104, 5, 274, 1 print(scalar(localtime)); # Fri Oct 1 15:05:32 2004 } # Refer to wantarray in perlfunc.
Re: while loop, search, and replace while loop, search & replace
by tmoertel (Chaplain) on Oct 01, 2004 at 18:01 UTC
    What you're doing is printing the result of the substitution operation, and that's not what you think it is. The s/// operator returns the number of substitutions it performed, which in your case is always 1.

    You want to print out the side effect of having performed the operation, which is captured in the variable $_ that your substitution operated on. Perform the substitution on $_ and then print $_.

    while (<IN>){ if (/^call started/) { s/^call.*total/Modem Line 0\/$i : /; print OUT "$_\n"; $i++; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-09-18 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls