Below is a quick attempt at doing some things in ruby that I have been
doing
in perl.
Question 1 how can I save and then reuse the functions that are defined
in
other applicaitons.
Question 2 how can I rewrite some this in a more ruby way
Itterating over an array and then having to have a counter variable to
make
“in-place” changes seems a little overkill and I am pretty certian there
is
probaly a better way.
I have a lot of pipe delimented text files that I work with that contain
some standard thing that always get converted. Dates and Numbers to name
a
few.
Thanks for you time !!
def splitpipe( string )
count = 0
record = string.split(’|’)
record.each do |element|
element = element.strip
element = formatnum(element) if element =~ /\d?.\d+/
element = convertdate(element) if element =~ /^\d\d/\d\d/\d\d$/
record[ count ] = element
count += 1
end
end
def formatnum(num)
num = num.to_f
end
def convertdate(date,type=0)
datearray = date.split(’/’)
if datearray[2].to_i > 90
datearray[2] = “19” + datearray[2]
else
datearray[2] = “20” + datearray[2]
end
if type == ‘human’
date = datearray.join("-")
elsif type == ‘array’
datearray
else
date = datearray[2] + datearray[0] + datearray[1]
end
end
salesfile = File.new( FILENAME, ‘r’ )
worksign = 1
objects = Array.new
salesfile.each_line do |line|
record = splitpipe( line.gsub(/^H:/,’’) )
if record[1] == ‘C’ or record[2] == ‘Y’
worksign = -1
record[9] = record[10] if record[1] == ‘C’ #Set qty shipped if
credit
work around
else
worksign = 1
end
record[6] = record[6] * worksign
record[7] = record[7] * worksign
record[9] = record[9] * worksign
record.each { |e| puts e }
exit
end
salesfile.close