Ville Jungman wrote:
> 
> Shortly, I think it might be good if loops (etc.) could return values.
> 
> Example 1: Retnext (like 'return next' borrowed from pl/sql)
> You want to extract numbers from an array if they are > 4 and put an
> 'a'-letter after them.
> 
>    @values=(1,3,5,7);
>    @bigger_than_4=                           # get an array from loop
>       foreach $value(@values) {
>          retnext $value."a" if $value > 4;   # return value from loop if > 4
>       }
>    ;

$ perl -le'
@values = qw/ 1 3 5 7 /;
@bigger_than_4 = grep { ( $_ = "${_}a" ) > 4 } @values;
print for @bigger_than_4;
'
5a
7a

And of course if you have warnings enabled (which you should) you can do
it like this:

@bigger_than_4 = do { no warnings 'numeric'; grep { ( $_ = "${_}a" ) > 4
} @values };


> Example 2: Retlast (== perl 'last'-command with a value)
> 
> You need to escape a loop with a value. Familiar way:
> 
>    while(<FH>){
>       if(/stop/){
>          $array_terminated='true';
>          last;
>       }
>    }
>    if($array_terminated){
>       # something
>    }
> 
> This could be written as:
> 
>    if(
>       while(<FH>){
>          retlast if /stop/;                  # returns $_ by default
>       }
>    ){
>       # something
>    }
> 
> So, not very conserverite but think what all you could do with this.
> And please, let me know what you think about this. Crap?

The value in $_ is not local to the while loop so that if you exit the
loop early with last, the value in $_ outside the loop will still be the
last value from inside the while loop.

while ( <FH> ) {
    last if /stop/;  # $_ contains "stop that!\n"
    }
print;  # will print "stop that!\n"



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to