in reply to Re: When does while set $_ ?
in thread When does while set $_ ?

Thanks, John. I did read the man pages, but didn't find this one. I guess I just naturally assumed that the while statement should assign $_.

Is there any (simple) way to assign the outer $_ inside the match() function when in a while-loop? Or would that involve some major hacking?

Replies are listed 'Best First'.
Re: Re: When does while set $_ ?
by jmcnamara (Monsignor) on Aug 01, 2002 at 10:02 UTC

    You could localise $_ as follows:
    while (local $_ = match('a')) { print "- $_\n"; }

    However, it would be better to use grep() instead of your match() function. Then all of your code could be replaced with the following line: ;-)     print "- $_\n" for grep {/a/} @data;

    --
    John.

      Thanks. Unfortunately, using grep() is not an option -- the original match() could involve multiple m// or a SQL query, depending on the data interface selected ;-).

      But I wonder how four grep()'s compare to four m//'s in a foreach loop... I guess the latter approach still wins, because the array needs to be traversed only once.