Sorry nother regexp/ruby problem

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

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

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)

Hmm 1/2 hour messing with sub, or 2 minutes - split + join(", ") perfect

Sorry again
Kev

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
}

···

On Tuesday 04 October 2005 02:29, Kev Jackson wrote:

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

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 '+');')

Why do you use VALUES in the regexp? I cannot see it in your sample data.
It would be easier if you post sample output that you want to see. I'm
not sure about the "first 4" elements (is it characters? is it items in
the list?).

Kind regards

    robert

Robert Klemme wrote:

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 '+');')
   
Why do you use VALUES in the regexp? I cannot see it in your sample data.
It would be easier if you post sample output that you want to see. I'm
not sure about the "first 4" elements (is it characters? is it items in
the list?).

Sorry about that, the original string is part of an insert statement
INSERT INTO TABLE (FIELD, FIELD, FIELD) VALUES (STUFF, STUFF, STUFF etc...);

···

--------------------------------------
                                                                              regexp to munge this bit

the first 4 elements would be '817-017', 800000, 0, 0

As someone else pointed out, split is a better tool for this job (and indeed having resolved the problem with split and join I now see my folly trying to use a regexp)

kev

just FYI here're reg ex tools that i've found helpful (not
ruby-specific, but PCRE / perl-compatible. Ruby RE has a few deltas
from perl, which escape me at the moment besides lookbehind:

http://www.weitz.de/regex-coach/
http://samuelfullman.com/team/php/tools/regular_expression_tester_p2.php

Kevin Brown wrote:

···

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

Kev Jackson wrote:

Robert Klemme wrote:

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 '+');')

Why do you use VALUES in the regexp? I cannot see it in your sample
data. It would be easier if you post sample output that you want to
see. I'm not sure about the "first 4" elements (is it characters?
is it items in the list?).

Sorry about that, the original string is part of an insert statement
INSERT INTO TABLE (FIELD, FIELD, FIELD) VALUES (STUFF, STUFF, STUFF
etc...);

--------------------------------------

regexp to munge this bit

the first 4 elements would be '817-017', 800000, 0, 0

As someone else pointed out, split is a better tool for this job (and
indeed having resolved the problem with split and join I now see my
folly trying to use a regexp)

If it's guaranteed that there are no "," in STUFF you might cook up a
regexp but split is certainly simpler.

    robert