in reply to split every other value

how about just:
my $scalar = '1,2,3,4,5,6,7,8'; print "$scalar\n"; my @evens; while ( $scalar =~ s/^([^,]),[^,](,|$)// ) { push @evens, $1; } print "@evens\n";

Replies are listed 'Best First'.
Re^2: split every other value
by Aristotle (Chancellor) on Aug 07, 2004 at 02:35 UTC

    You don't need to use a substitution if you anchor the pattern using \G. And why not capture the odd elements as well?

    my ( @even, @odd ); while ( $text =~ /\G ( [^,]+ ) (?: , ( [^,]+ ) )? (?: , | \z ) /gx ) +{ push @even, $1; push @odd, $2 if defined $2; }

    Makeshifts last the longest.