Read column from text or csv file and append "varchar,"

Hi Guys,

I'm trying to use a ruby script to read a column of data from a single
text file or csv and put it into an array which i can print out and also
append "varchar," after each hash entry.

For example

if i had

test
12
312
soon
fast
bob

I want it to turn into

bob varchar, 12 varchar, 312 varchar, soon varchar, fast varchar, bob
varchar

How would this be possible?

···

--
Posted via http://www.ruby-forum.com/.

Place text into an array and make varchar a constant zap

···

Sent from my iPhone

On Aug 1, 2013, at 10:00 AM, "Talal B." <lists@ruby-forum.com> wrote:

Hi Guys,

I'm trying to use a ruby script to read a column of data from a single
text file or csv and put it into an array which i can print out and also
append "varchar," after each hash entry.

For example

if i had

test
12
312
soon
fast
bob

I want it to turn into

bob varchar, 12 varchar, 312 varchar, soon varchar, fast varchar, bob
varchar

How would this be possible?

--
Posted via http://www.ruby-forum.com/\.

One way to do it:

$ cat data.txt
test
12
312
soon
fast
bob

$ irb
2.0.0-p247 :001 > data = File.readlines('data.txt')
=> ["test\n", "12\n", "312\n", "soon\n", "fast\n", "bob\n"]
2.0.0-p247 :002 > data.map! {|entry| "#{entry.chomp} varchar" }
=> ["test varchar", "12 varchar", "312 varchar", "soon varchar", "fast
varchar", "bob varchar"]
2.0.0-p247 :003 > data.join(', ')
=> "test varchar, 12 varchar, 312 varchar, soon varchar, fast varchar,
bob varchar"

Regards,
Marcus

···

Am 01.08.2013 17:00, schrieb Talal B.:

Hi Guys,

I'm trying to use a ruby script to read a column of data from a single
text file or csv and put it into an array which i can print out and also
append "varchar," after each hash entry.

For example

if i had

test
12
312
soon
fast
bob

I want it to turn into

bob varchar, 12 varchar, 312 varchar, soon varchar, fast varchar, bob
varchar

How would this be possible?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A