Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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);

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://395669]
Approved by ikegami
[thezip]: Where did you go? We've already set a record for all-time February rainfall here.
[Corion]: thezip: The Canary Islands - they have a constant climate
[Corion]: Maybe a bit too many old people, but for some do-nothing vacation for R&R exactly the right thing ;)=
[thezip]: That's awesome. I bet you feel totally rejuvenated and ready for the next thing.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-02-23 17:05 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (349 votes). Check out past polls.