[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple regexp question

Ak 756

9/28/2007 3:31:00 AM

Hi

I have a .txt file which contains one line data as

"George",33,"engineer, r&d department, this company","234-7554-344"

I use name,age,memo,tel=File.open(filename).split(',') to collect values
but get wrong value for memo and tel. I want string "engineer, r&d
department, this company" not be splited by comma, how can I do that?
--
Posted via http://www.ruby-....

4 Answers

Cam

9/28/2007 4:39:00 AM

0

Hello,

On 9/27/07, Ak 756 <macro.peng@gmail.com> wrote:
> I have a .txt file which contains one line data as
>
> "George",33,"engineer, r&d department, this company","234-7554-344"
>
> I use name,age,memo,tel=File.open(filename).split(',') to collect values
> but get wrong value for memo and tel. I want string "engineer, r&d
> department, this company" not be splited by comma, how can I do that?

I'm sure there's a more elegant solution, but this worked for me
(assuming the string is stored in the variable s):

name, age, memo, tel = $1, $2, $3, $4 if s =~
/^"(.+?)",(\d+),"(.+?)","([-\d+]+)"/

Cam

Heesob Park

9/28/2007 5:04:00 AM

0

Hi,

Ak 756 wrote:
> Hi
>
> I have a .txt file which contains one line data as
>
> "George",33,"engineer, r&d department, this company","234-7554-344"
>
> I use name,age,memo,tel=File.open(filename).split(',') to collect values
> but get wrong value for memo and tel. I want string "engineer, r&d
> department, this company" not be splited by comma, how can I do that?

Why not use csv module?

require 'csv'
data = '"George",33,"engineer, r&d department, this
company","234-7554-344"'
name,age,memo,tel=CSV.parse_line(data)

Regards,

Park Heesob
--
Posted via http://www.ruby-....

Ak 756

9/28/2007 5:47:00 AM

0

Heesob Park wrote:
> Why not use csv module?
>
> require 'csv'
> data = '"George",33,"engineer, r&d department, this
> company","234-7554-344"'
> name,age,memo,tel=CSV.parse_line(data)
>
> Regards,
>
> Park Heesob

Hi Heesob

Thanks very much.
Great csv module! Everything becoms so easy with it. -:)
--
Posted via http://www.ruby-....

Ari Brown

9/28/2007 11:23:00 AM

0


On Sep 27, 2007, at 11:31 PM, Ak 756 wrote:

> Hi
>
> I have a .txt file which contains one line data as
>
> "George",33,"engineer, r&d department, this company","234-7554-344"
>
> I use name,age,memo,tel=File.open(filename).split(',') to collect
> values
> but get wrong value for memo and tel. I want string "engineer, r&d
> department, this company" not be splited by comma, how can I do that?

fastercsv might help. It's great, AND a lot faster for parsing comma
separated values.

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.