Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Save all but line 32!

by Abigail-II (Bishop)
on Sep 18, 2002 at 16:25 UTC ( #198898=note: print w/replies, xml ) Need Help??


in reply to Re: Save all but line 32!
in thread Save all but line 32!

Penny wise but pound foolish.

Your program will read in all lines after line 32 before printing them. Usually that will be a lot slower than the comparison you save.

I do not understand your remark about $/. You aren't setting it, nor does any of the switches influence it. How could $/ be any different from the default?

My suggestion for the program:

perl -i -nwe 'print unless 32 .. 32' filename
Abigail

Replies are listed 'Best First'.
Re: Re: Save all but line 32!
by cowens (Beadle) on Sep 18, 2002 at 17:25 UTC
    This confuses me. I don't see how this would work. It looks like it will print no lines since 32 .. 32 should return either 1 (the number of elements in the list) or 32 (the last element in the list) both of which are "true". I am betting the later, but I am not sure. Would you kindly point out where I am wrong?
    perl -- executes perl
    -i   -- in-place editing
    -n   -- implict loop
    -w   -- turn on warnings (why?)
    -e   -- execute the following string as the program
    
    print    -- print what is in $_ (only executed if the unless is  false)
    unless   -- execute the previous command if the result of the following
                expression is 0, undef, or '' (or the equivalent)
    32 .. 32 -- construct a list containing the numbers 32 through 32
    filename -- the file to edit
    
      You don't understand context. An expression in scalar context doesn't mean it's evaluated in list context, and then the list is evaluated in scalar context.

      No, it means the expression is evaluated in scalar context. And .. doesn't act at all the same in scalar context as in list context. From the manual page:

      In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various edi- tors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated. It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once. If you don't want it to test the right operand till the next evaluation, as in sed, just use three dots ("...") instead of two. In all other regards, "..." behaves just like ".." does. The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evalu- ated while the operator is in the "true" state. The precedence is a little lower than || and &&. The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encountered. The final sequence number in a range has the string "E0" appended to it, which doesn't affect its numeric value, but gives you something to search for if you want to exclude the end- point. You can exclude the beginning point by waiting for the sequence number to be greater than 1. If either operand of scalar ".." is a constant expression, that operand is implicitly compared to the $. variable, the current line number. Examples: As a scalar operator: if (101 .. 200) { print; } # print 2nd hundred lines next line if (1 .. /^$/); # skip header lines s/^/> / if (/^$/ .. eof()); # quote body
      Abigail

        I've read those paragraphs at least a dozen times since I first saw reference to the "flip-flop operator" in the spoiler for an obfu, one of erudil's or maybe on of yours, and I still have trouble applying it.

        If anyone has a less wordy, more .. um .. transparent description of the .. and ... in a scalar context, or even better a simple example of use that is self-contained and demonstrative, I for one would find that very useful.


        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-10-31 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (291 votes). Check out past polls.

    Notices?