[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: text processing library

James Gray

4/17/2007 12:45:00 PM

On Apr 17, 2007, at 1:40 AM, Stephen Smith wrote:

> So here's the problem: I have a CSV file that I need to munge into
> a batch
> file for a mainframe to process. This file has many (say, 30 or
> more) fixed
> width fields per record with distinct rules attached to each field
> (e.g.,
> field1 is an eight-position date following the pattern YYYYMMDD,
> field2 is a
> five-position enumerated customer type, field3 may contain either a
> 70 or a
> 71 depending on the customer type, etc).

I'm a little confused by your description of the file. You call it a
CSV file and say it has fixed-width fields, but those are two
different things.

Either way though, Ruby has the tools you need.

For CSV data, see the standard "csv" library. For splitting up fixed
width fields, a call to String#unpack will do.

Hope that helps.

James Edward Gray II


2 Answers

Alex LeDonne

4/17/2007 4:05:00 PM

0

On 4/17/07, Stephen Smith <4fires@gmail.com> wrote:
[snip]
> Your FasterCSV and String#unpack seem like a great place to start. At a
> minimum, I need to be able to attach rules to each field like the number of
> positions (as well as packing character, eg, whitespace or zeroes, and maybe
> formulas based on other field values) in my format string. Is there an
> equivalent of String#pack out there?
>
> Steve
>

Array#pack

So unpack goes from String to Array, and pack goes from Array to String.

-A

James Gray

4/17/2007 4:56:00 PM

0

On Apr 17, 2007, at 10:49 AM, Stephen Smith wrote:

> I frequently have to turn CSV files into files that follow different
> fixed-width formats.

Ah, I understand now. Your project is to translate CSV to fixed-
width. Got it.

Obviously I'm biased, but FasterCSV should give you rich handling on
the reading end, I think. That part should be pretty covered.

Where you are likely to spend the effort is in the fixed-width
writing. There is an Array#pack, as others have pointed out, but you
sound like you're after something higher level than that. You want
it to catch the datas and pack them as YYYYMMDD strings for you and
the like.

If you come up with a good solution it may be worth generalizing and
sharing, in my opinion.

James Edward Gray II