From: Vraj Mohan Date: 2005-04-14T22:44:49+09:00 Subject: Re: Weaving Arrays galizur@gmail.com wrote: > 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[i] << format[i] > 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. > -- > Chris Long, San Diego Padres, 100 Park Boulevard, San Diego CA 92101 > > Score: 0, Diff: 1, clong killed by a Harvard Math Team on 1 > > > Have you looked at Array#zip?