in reply to Concatenating Array elements
Others have explained why the code you posted is not giving you the results you expect and have pointed out that splice is perhaps the tool for what you are trying to do. Because splice modifies the length of an array, and consequently the index values for elements to the right of the operation, it is best if operating on more than one element to work from right to left. You can use reverse to achieve this.
$ perl -Mstrict -Mwarnings -E ' my @arr = q{0} .. q{9}; for my $idx ( grep { $_ % 2 } reverse 0 .. $#arr ) { $arr[ $idx - 1 ] .= splice @arr, $idx, 1; } say for @arr;' 01 23 45 67 89 $
I hope this is helpful.
Cheers,
JohnGG
|
|---|