http://qs321.pair.com?node_id=380804


in reply to Re^2: split every other value
in thread split every other value

I shouldn't have used the names "@odd" and "@even", seeing as how we're supposed to be dealing with text. So here's a program which doesn't use those terribly misleading names, and deals with undefs.

#!/usr/bin/perl use strict; use warnings; my @array = map{chr} ('32' .. '126'); push @array, undef, undef; unshift @array, undef, undef, undef; # split elements into two arrays my (@arr0, @arr1); while (@array > 1) { my ($el0, $el1) = splice @array, 0, 2; push @arr0, $el0; push @arr1, $el1; } # get the last element if there is one if(@array) { push @arr0, shift @array; } # check that all elements were gotten die "still elements left" if (@array); print "arr0 = @arr0\narr1 = @arr1\n"; __END__