Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Date Format Change - Help request

Reply
Thread Tools

Date Format Change - Help request

 
 
Snoopy Dog
Guest
Posts: n/a
 
      04-26-2007
Newbie question about changing a date format

I have a series of data files in the format:
ABC, 7/21/2005, 1825
CNC1, 7/28/2005, 34.21
BPR, 9/3/2006, 34872

I need to output it to a DIFFERENT Directory as
ABC, 20050721, 1825
CNC1, 20050728, 34.21
BPR, 20060903, 34872

code snippet below

Find.find(sourcedir) do |path|
if File.file? path
puts path
log.puts path
output = path.gsub(sourcedir, destdir) # name/dir change for
output file
data = File.readlines(path)
data.each { |row|
(machine, date, produced) = row.split(/,/)
(tmonth, tday, tyear) = date.split(/\//)
newdate = tyear + "%02d" % tmonth + "%02d" % tday
date = newdate
log2.puts machine + " , "+ date + " , " + newdate
}
log.puts "Outputting file " + output
open(output,'w') {|f| f.puts data}
end
end

I get the file output to the proper location, but the DATE format does
not change.
The log2 shows output in the proper format, the date and newdate ARE the
same.
The output file is still the same as the original input file.

Not sure what I am missing, but any assistance is appreciated.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Dan Zwell
Guest
Posts: n/a
 
      04-27-2007
Hi,

the #each method does not modify the contents of the item that is being
looped over. I would use #map. You have the data you want to modify on
this array, so call data.map {|row| ...} and do your text transformation
inside the block. The last line is what counts, so to transform data[]
as you have described, the last line in the block should be 'machine + "
, "+ date + " , " + newdate', you don't need "puts" or anything.

Dan

Snoopy Dog wrote:
> Newbie question about changing a date format
>
> I have a series of data files in the format:
> ABC, 7/21/2005, 1825
> CNC1, 7/28/2005, 34.21
> BPR, 9/3/2006, 34872
>
> I need to output it to a DIFFERENT Directory as
> ABC, 20050721, 1825
> CNC1, 20050728, 34.21
> BPR, 20060903, 34872
>
> code snippet below
>
> Find.find(sourcedir) do |path|
> if File.file? path
> puts path
> log.puts path
> output = path.gsub(sourcedir, destdir) # name/dir change for
> output file
> data = File.readlines(path)
> data.each { |row|
> (machine, date, produced) = row.split(/,/)
> (tmonth, tday, tyear) = date.split(/\//)
> newdate = tyear + "%02d" % tmonth + "%02d" % tday
> date = newdate
> log2.puts machine + " , "+ date + " , " + newdate
> }
> log.puts "Outputting file " + output
> open(output,'w') {|f| f.puts data}
> end
> end
>
> I get the file output to the proper location, but the DATE format does
> not change.
> The log2 shows output in the proper format, the date and newdate ARE the
> same.
> The output file is still the same as the original input file.
>
> Not sure what I am missing, but any assistance is appreciated.
>


Find.find(sourcedir) do |path|
if File.file? path
puts path
log.puts path
output = path.gsub(sourcedir, destdir) # name/dir change for
output file
data = File.readlines(path)
data.each { |row|
(machine, date, produced) = row.split(/,/)
(tmonth, tday, tyear) = date.split(/\//)
newdate = tyear + "%02d" % tmonth + "%02d" % tday
date = newdate
log2.puts machine + " , "+ date + " , " + newdate
}
log.puts "Outputting file " + output
open(output,'w') {|f|
data.each{|row|
(machine, date, produced) = row.split(/,/)
(tmonth, tday, tyear) = date.split(/\//)
newdate = tyear + "%02d" % tmonth + "%02d" % tday
date = newdate
f.write
}
}
end
end

 
Reply With Quote
 
 
 
 
Snoopy Dog
Guest
Posts: n/a
 
      04-27-2007
Dan Zwell wrote:
> Hi,
>
> the #each method does not modify the contents of the item that is being
> looped over. I would use #map. You have the data you want to modify on
> this array, so call data.map {|row| ...} and do your text transformation
> inside the block. The last line is what counts, so to transform data[]
> as you have described, the last line in the block should be 'machine + "
> , "+ date + " , " + newdate', you don't need "puts" or anything.
>
> Dan
>



THANK YOU DAN

I ended up using

data.map! (|row|

as the control and added the line
machine + ", " + newdate + ", " + produced

as the last line in the block to get my desired result.

Thanks for your help

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM



Advertisments