From: Martin DeMello Date: 2003-07-12T15:20:24+09:00 Subject: Re: cross-corr time/array lag problem Qubert wrote: > I am starting to find the "Way" now, but I have a problem. > I am running cross correlations using Ruby, so first I set up the time > series data to be in columns with rows being the same time. > Now I am trying to lag one column to the other by: > a = [0, 1, 2, 3, 4, 5] > b = a.each_index do |j| > a[j-1] > end > > But afterward a = b with no lag. > a print a[j]," ",a[j-1] shows this works > but b is being stored as the original "a" not a modified "a" with an > hour lag. > > Why is b not being set with a lag? I can not .collect because I need > to manupilate the index not the contents of the array. each_index doesn't have an implicit collect, and its return value is the array itself (not sure why, but I can't remember needing a return value anyway). What you need is: b = [] a.each_index {|j| b[j] = a[j-1]} or, b = a.dup.unshift(nil) or (drumroll) b = a.map_with_index {|e, i| a[i-1]} had we a map with index which we don't (: though beware of wraparound - a[-1] returns the last element. martin