From: ahoward Date: 2003-06-17T12:40:35+09:00 Subject: Re: Extracting columns from Array withSubstitution problem On Tue, 17 Jun 2003, Qubert wrote: > I have a little hurdle to jump on this code. I am manipulating a lot of > data that is in a columnar format. The first row has the titles of each > column that I successfully use to call the columns through the command line > (ARGV.shift). Below I show 2 attempts to pull out the columns of data using > the reference to the first row. The first one (y1) works, until I found > that absent data was marked by one or more *. So y1 was changing *** to 0, > but I need to input these missing data points as -9. So y2 was attempted, > in which it finds the ** but then for some reason inputs the entire array x > at the end of the newly collected array. Now I didn't believe that it would > work, but I didn't expect it to input array x either. So I would like some > help creating code that will collect a new array like y1 but substitute -9 > for ***. > Partial code: > arg1 = ARGV.shift > arg2 = ARGV.shift > label = Array.new # for y1, y2 I found I didn't need to declare y1=Array.new? > label = x.shift # first line is headers for the columns > y1 = x.collect {|j| j[label.index(arg1)].to_i} # works but changes ** into 0 > y2 = x.each do |j| # doesn't work, inputs all of x as y2, i.e. y2 = x > � if /\*+/ =~ j[label.index(arg2)] > ��� -9 > else > j[label.index(arg2)].to_i > � end > end * reverse the args to '=~' eg. if /\*+/ =~ j[label.index(arg2)] becomes if j[label.index(arg2)] =~ /\*+/ > Thank you for the help, Ruby is great, I just haven't found the "Way". me neither - i just keep searching. ;-) btw. i do stuff like this all the time and made a *tiny* little module which helps. although some may argue my stuff is crap, the RAA is great - using it is the ruby way! #!/usr/bin/env ruby #http://raa.ruby-lang.org/list.rhtml?name=array-fields require 'arrayfields' rows = DATA.readlines.map{|row| row.split} fields = rows.shift rows.fields = fields p rows[0]['DIR'] # "030" p rows[0]['DIR', 'CLG'] # ["030", "722"] __END__ DIR SPD GUS CLG 030 6 *** 722 360 5 *** 722 020 * *** 722 030 6 *** 722 -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 | ~ > ruby -e 'p(%.\x2d\x29..intern)' ====================================