G
galizur
Any suggestions on a quick way to weave two arrays?
To split up a file with fixed width columns, and to then insert
a tab after each one, is fast and easy:
File.open($*[0]).each do |raw_line|
line = raw_line.chomp.unpack($format)
last = line.pop
$stdout << line*"\t" << last << "\n"
end
But what if I wanted to do something more general, such as weaving
two arrays?
This works:
File.open($*[0]).each do |raw_line|
line = raw_line.chomp.unpack($split)
line.each_index do |i|
$stdout << line << format
end
end
There must be a speedier way. Ideally, it'd be nice to be able to
do something like line*format, with the product the weave of the two
arrays. If the second array is shorter, repeat from the beginning.
This would be an extension of the way array*string currently works.
To split up a file with fixed width columns, and to then insert
a tab after each one, is fast and easy:
File.open($*[0]).each do |raw_line|
line = raw_line.chomp.unpack($format)
last = line.pop
$stdout << line*"\t" << last << "\n"
end
But what if I wanted to do something more general, such as weaving
two arrays?
This works:
File.open($*[0]).each do |raw_line|
line = raw_line.chomp.unpack($split)
line.each_index do |i|
$stdout << line << format
end
end
There must be a speedier way. Ideally, it'd be nice to be able to
do something like line*format, with the product the weave of the two
arrays. If the second array is shorter, repeat from the beginning.
This would be an extension of the way array*string currently works.