Given two array refs, this will interleave their elements, returing one long array. Handy for taking two arrays (@keys, @values) and combining them into a hash.
"Extra" elements will be placed at the end.
sub interleave { return @{$_[0]} unless @{$_[1]}; return @{$_[1]} unless @{$_[0]}; return shift(@{$_[0]}), shift(@{$_[1]}), &interleave; } print interleave( ['a'..'e'], [1..5] ), "\n"; my %hash = interleave( ['a'..'e'], [1..5] );
In reply to interleave two arrays by eg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |