[comp.lang.perl] Deleting

emv@math.lsa.umich.edu (Edward Vielmetti) (07/02/90)

In article <1990Jun30.214249.15211@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:

   | Solutions other than those using 'perl' are preferred.

   But, since you didn't say *necessary*, how about:

   perl -ne 'print unless /^$/ && $once++;'

hm, nice.  how about this problem: printing the first 10 lines after
a signal line?  I would think it could be done with the .. operator, namely

	perl -ne 'if (/===/) {$l = $. + 10 ;} ; if (/===/ .. $l) {print;} '

but this prints only the === line and the one following.  a test with digits
shows that it works as expected for this:

	perl -ne 'if (/===/) {$l = $. + 10 ;} ; if (/===/ .. 50) {print;} '

which lead me to experiment with the following which worked

	perl -ne 'if (/===/) {$l = $. + 10 ;} ; eval "if (/===/ .. $l) {print;}"; '

the eval appears to be neccesary in order to force the .. to be
interpreted in a scalar context, but I guess I don't really understand
the man page, & in retrospect a simple loop would have done the trick
OK.

--Ed

Edward Vielmetti, U of Michigan math dept <emv@math.lsa.umich.edu>

merlyn@iwarp.intel.com (Randal Schwartz) (07/02/90)

In article <EMV.90Jul1175629@urania.math.lsa.umich.edu>, emv@math (Edward Vielmetti) writes:
| hm, nice.  how about this problem: printing the first 10 lines after
| a signal line?

Ambiguous spec.  I'm presuming you mean "eleven total lines, including
the trigger line", since your code seems to want to do that.

perl -ne 'print if /===/ ? ($c = 11) : (--$c > 0)'

($c if positive is the number of lines left to be printed, including
the current one.)

$x=25;print substr(',rekcah lreP rehtona tsuJ',$x,1) while --$x >= 0
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/03/90)

In article <EMV.90Jul1175629@urania.math.lsa.umich.edu> emv@math.lsa.umich.edu (Edward Vielmetti) writes:
: In article <1990Jun30.214249.15211@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: 
:    | Solutions other than those using 'perl' are preferred.
: 
:    But, since you didn't say *necessary*, how about:
: 
:    perl -ne 'print unless /^$/ && $once++;'
: 
: hm, nice.  how about this problem: printing the first 10 lines after
: a signal line?  I would think it could be done with the .. operator, namely
: 
: 	perl -ne 'if (/===/) {$l = $. + 10 ;} ; if (/===/ .. $l) {print;} '
: 
: but this prints only the === line and the one following.  a test with digits
: shows that it works as expected for this:
: 
: 	perl -ne 'if (/===/) {$l = $. + 10 ;} ; if (/===/ .. 50) {print;} '
: 
: which lead me to experiment with the following which worked
: 
: 	perl -ne 'if (/===/) {$l = $. + 10 ;} ; eval "if (/===/ .. $l) {print;}"; '
: 
: the eval appears to be neccesary in order to force the .. to be
: interpreted in a scalar context, but I guess I don't really understand
: the man page, & in retrospect a simple loop would have done the trick
: OK.

The eval doesn't force a scalar context--it forces $l to be a static literal.
The arguments to .. are ordinarily interpreted as *boolean expressions*.
However

    If either operand of scalar .. is static, that operand is implicitly
    compared to the $. variable, the current line number.

So /===/ .. 50 is interpreted as /===/ .. ($. == 50).  But /===/ .. $l isn't.

The scalar .. is in there mostly to emulate sed.  Myself, I'd probably
go ahead and write a loop too.

Larry