zork42 has asked for the wisdom of the Perl Monks concerning the following question:
I'm completely mystified by this.
In the 3 example code blocks below I'm pushing the same things onto an array.
When I push the result of a failed match, nothing is pushed onto the array (unless I first save the result in a variable).
Why is this?!
Thanks!
use strict; use warnings; my @foo; push @foo, 1; print scalar @foo, "\n"; # output +: 1 push @foo, 'abc' =~ /abc/; print scalar @foo, "\n"; # output +: 2 push @foo, 'abc' =~ /abcd/; print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, ''; print scalar @foo, "\n"; # output +: 3 push @foo, 0; print scalar @foo, "\n"; # output +: 4 push @foo, undef; print scalar @foo, "\n"; # output +: 5 @foo = (); print "\n"; push @foo, (1); print scalar @foo, "\n"; # output +: 1 push @foo, ('abc' =~ /abc/); print scalar @foo, "\n"; # output +: 2 push @foo, ('abc' =~ /abcd/); print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, (''); print scalar @foo, "\n"; # output +: 3 push @foo, (0); print scalar @foo, "\n"; # output +: 4 push @foo, (undef); print scalar @foo, "\n"; # output +: 5 @foo = (); my $bar; print "\n"; $bar = 1; push @foo, $bar; print scalar @foo, "\n"; + # output: 1 $bar = 'abc' =~ /abc/; push @foo, $bar; print scalar @foo, "\n"; + # output: 2 $bar = 'abc' =~ /abcd/; push @foo, $bar; print scalar @foo, "\n"; + # output: 3 - this time its been added to @foo $bar = ''; push @foo, $bar; print scalar @foo, "\n"; + # output: 4 $bar = 0; push @foo, $bar; print scalar @foo, "\n"; + # output: 5 $bar = undef; push @foo, $bar; print scalar @foo, "\n"; + # output: 6
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Result of failed regexp match not pushed onto array
by haukex (Archbishop) on Feb 16, 2019 at 12:26 UTC | |
|
Re: Result of failed regexp match not pushed onto array
by AnomalousMonk (Archbishop) on Feb 16, 2019 at 19:50 UTC | |
|
Re: Result of failed regexp match not pushed onto array
by zork42 (Monk) on Feb 20, 2019 at 11:46 UTC |