develooper Front page | perl.fwp | Postings from July 2003

Re: range operator for a condition in 2 steps

Thread Previous | Thread Next
From:
Bart Lateur
Date:
July 31, 2003 04:08
Subject:
Re: range operator for a condition in 2 steps
Message ID:
t6thivcejhpsqtrd0dmp4ml2hj466od8nb@4ax.com
On Wed, 30 Jul 2003 21:36:46 -0500, James E. Tilley wrote:

>Does
>
>  if (/^BEGIN$/ .. (/^\s*$/ .. /^END$/) =~ /E/)
>
>do what you want?

s/BEGIN/START/ to plug it in the skeleton.

And yes, that's perfect. Quite elegant too. Very much so.


Here's my own solution:

	my $pre;
	while(<DATA>) {
	    if(my $count = (($pre = /^START$/) .. 
	    	(/^$/ && undef $pre, !$pre && /^END$/))) {
	        print "$count: $_";
	    }
	}

What I don't particularly like about it, is how it needs an extra outer
scope variable, $pre, to hold a flag to store the intermediate
condition.

BTW with the additional variable $count capturing the result of the
expression,  it's easier to keep track of what happens.

-- 
	Bart.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About