[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

Harry

3/30/2007 5:25:00 AM

On 3/30/07, Stephen Smith <4fires@gmail.com> wrote:
>
>The fields are fixed widths, each record is separated by a new line.
> I need to get this data into csv format so I can import it into a mysql
> database for analysis.
>
> I figure a script is the best way, but I'm stumped since the fields aren't
> delimited.
>
> Any tips, suggestions, pointers or a script that does something similar
> would be GREATLY appreciated.
>

This is sloppy but you get the idea.
Try something like this. You can improve it.

arr = ["abcdefgh","12345678","867-5309"]
mydata = ""
arr.each do |x|
x =~ /(...)(.)(....)/ #grabs 3 characters, then 1, then 4
mydata << $1 + "," + $2 + "," + $3 + "\n"
end

puts mydata


Harry

--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

4 Answers

Gary Wright

3/30/2007 5:50:00 AM

0


On Mar 30, 2007, at 1:25 AM, Harry wrote:
> x =~ /(...)(.)(....)/ #grabs 3 characters, then 1, then 4
> mydata << $1 + "," + $2 + "," + $3 + "\n"

mydata << /(...)(.)(....)/.match(x).captures.join(',') << "\n"

Gary Wright

Harry

3/30/2007 6:24:00 AM

0

On 3/30/07, Gary Wright <gwtmp01@mac.com> wrote:
>
> On Mar 30, 2007, at 1:25 AM, Harry wrote:
> > x =~ /(...)(.)(....)/ #grabs 3 characters, then 1, then 4
> > mydata << $1 + "," + $2 + "," + $3 + "\n"
>
> mydata << /(...)(.)(....)/.match(x).captures.join(',') << "\n"
>
> Gary Wright
>

'captures' is new to me.
Thanks.

Harry

--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

Andrew Stewart

3/30/2007 8:28:00 AM

0


On 30 Mar 2007, at 06:49, Gary Wright wrote:
> On Mar 30, 2007, at 1:25 AM, Harry wrote:
>> x =~ /(...)(.)(....)/ #grabs 3 characters, then 1, then 4
>> mydata << $1 + "," + $2 + "," + $3 + "\n"
>
> mydata << /(...)(.)(....)/.match(x).captures.join(',') << "\n"

If your fixed-width fields are more than, say, five characters wide,
your regexp would be clearer by specifying the number of occurrences
of each group rather than writing a . for each occurrence. I.e.:

mydata << /(.{3})(.)(.{4})/.match(x).captures.join(',') << "\n"

Regards,
Andy Stewart

Peña, Botp

3/30/2007 10:52:00 AM

0

From: Stephen Smith [mailto:4fires@gmail.com] :
# Nice. You just made my month.
# Thank you Harry. Thank you Gary.
# Freekin' elegant.
# Steve
#
# On 3/29/07, Harry <ruby.hardware@gmail.com> wrote:
# > On 3/30/07, Gary Wright <gwtmp01@mac.com> wrote:
# > > On Mar 30, 2007, at 1:25 AM, Harry wrote:
# > > > x =~ /(...)(.)(....)/ #grabs 3 characters, then 1, then 4
# > > > mydata << $1 + "," + $2 + "," + $3 + "\n"
# > >
# > > mydata << /(...)(.)(....)/.match(x).captures.join(',') << "\n"

ruby is fun, elegant, and powerful.
some more samples.

irb(main):030:0> x
=> "01234567890"
irb(main):031:0> x[/(..)...(....)/]
=> "012345678"
irb(main):032:0> $1
=> "01"
irb(main):033:0> $2
=> "5678"
irb(main):034:0> x[0..1]
=> "01"
irb(main):035:0> x[5..8]
=> "5678"
irb(main):036:0> field1=0..1
=> 0..1
irb(main):037:0> field2=5..8
=> 5..8
irb(main):038:0> x[field1]
=> "01"
irb(main):039:0> x[field2]
=> "5678"
irb(main):040:0>

kind regards -botp