From: "Peña, Botp" Date: 2007-08-25T16:40:43+09:00 Subject: Re: add outside variable to array elements From: Ignatz Hafflegab [mailto:mcpeople@hotmail.com] # whole_file = open('16bytes', 'rb') { |f| f.read} # ar1 = whole_file.unpack('S*') # puts('Enter an increase value') # incVal=gets.chomp # incVal = incVal.to_i 2 lines above can be merged like, incVal = gets.to_i # ar2 = ar1.collect { |x| x + incVal} # ar2= ar2.pack('S*') # open('output','wb'){|f| f.write ar2} # # Is there any way to iterate over an array and apply a conditional like # if x < 45000 then x = x + incVal refer back to the line containing ar2 = ar1.collect { |x| x + incVal} change it to any of the ff (pls test all) ar2 = ar1.collect { |x| x<45000 ? (x + incVal) : x} or ar2 = ar1.collect { |x| if x<45000 then (x + incVal) else x end} or ar2 = ar1.select{|x| x<45000}.collect{|x| x + incVal} i usually prefer the last one, since i think like: "select those elements < 45000, then with those collections, increment each". but that is just me. welcome to ruby. kind regards -botp