On Tuesday 04 October 2005 02:29, Kev Jackson wrote:
> Given the following data
>
> '817-017', 800000, 0, 0, 200000, 200000, 500000, 500000, 500000, 500000,
> 60000, 59122, 0, 800000, '817'
>
> How can I easily just select the first 4 elements (where elements 2,3
> and 4 could be NULL)?
>
> At the moment I'm trying a regexp of the form
>
> line.sub!(/(VALUES \(\s*'\w',\s*[0-9]+,\s*[0-9]+,\s*[0-9]+|NULL).+/, '\1
> '+');')
>
> I'm thinking that a split will be more productive, but then I've got to
> merge everything back together, sub + friends seems to be easier
> (although I'm struggling to get exactly what I want as output)
>
> Any thoughts + best practices/idioms etc would be very much appreciated

I'd go with split

data = split(',', "'817-017', 800000, 0, 0, 200000, 200000, 500000, 500000, 
500000, 500000, 60000, 59122, 0, 800000, '817'")

# process here

data.each{ |part|
  string << part
}

> Kev
>
> PS sorry to repeatedly spam on about this, but I have a deadline and I'd
> rather be using Ruby than C, Java, Python, Perl