Working on a first plain Ruby script, need help with `join'

J

Jesse Crockett

I need help in the block to create a csv file of these entries.

the file, abridged:

@verb_index = "

aah v 1 1 @ 1 0 00865776
abacinate v 1 1 @ 1 0 02168378
abandon v 5 4 @ ~ $ + 5 5 02228031 02227741 02076676 00613393 00614057
abase v 1 3 @ ~ + 1 0 01799794
abash v 1 3 @ ~ + 1 0 01792097
 
M

Martin DeMello

n.split
n.join(", ") # fails

n.split returns a new object containing the array of split entries
from n. you aren't assigning this new object to anything, so it is
silently discarded. the object pointed to by n is unchanged. what you
want is something like

a = n.split
a.join(", ")

or more compactly

n.split.join(", ')

martin
 
J

Jesse Crockett

عمر ملقب بالثانی said:
Do you want to do n.split.join(", ") ?

Yes. How do I put them into a CSV file? Thank you.
 
Ø

عمر ملقب بالثانی

Jesse said:
Yes. How do I put them into a CSV file? Thank you.

Supposing you're reading from stdin or from files passed as argument and
you're writing to stdout:

ARGF.each { |line| puts line.split.join(', ') }

If you want to output to file

output_file = open ('output.cvs', 'w')
ARGF.each { |line| output_file.puts line.split.join(', ') }

[a beginner in Ruby]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top