Dr. Claus-Peter Becke wrote:
[...]
foreach (/(\w+)/i) {
push @words,$&;
}
print    $q->popup_menu('to_thesaurus', @words);
[...]

Use the /g (global) option to the match operator, and push $_ onto @words rather than $&:

foreach (/(\w+)/ig) {
        push @words, $_;
}

Or ditch the 'foreach' and do it more simply:

push @words, /(\w+)/ig;

P.S.
You probably got confused between 'while' and 'foreach.' Foreach collects all of the lists elements together before executing the block, whereas 'while' performs a test, and executes the block if the test is true. When using 'foreach' the all of the matches have finished before your block is executed. That's why you only get the last match if you use $&. But 'while' does not collect a list beforehand, so you can do this:

while (/(\w+)/ig) {
    push @words, $&;
}

And you still get the correct result (tested). But change that 'while' to a 'foreach' and watch it fall over.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to