in reply to Is there any way to make an array non-greedy?
You have a bug:
my $string = join('x', 'a'x20);
should be
my $string = join('x', ('a')x20);
Fix that and it works.
If you always have one element left over, you can make the code readable as:
use strict; use warnings; my $string = join('x', ('a')x20); my @foo = split('x', $string); my $foo = pop(@foo); print "Success!\n" if $foo eq "a";
If you always have a variable number of elements left over, you have a problem fixed as follows:
use strict; use warnings; my $string = join('x', ('a')x20); my (@foo, $foo); (@foo[0..18], $foo) = split('x', $string, 20); # <-- added limit. print "Success!\n" if $foo eq "a";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is there any way to make an array non-greedy?
by thor (Priest) on Oct 14, 2005 at 18:25 UTC | |
by duff (Parson) on Oct 15, 2005 at 03:56 UTC |