Forum: Ruby split words with two columns

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dani (Guest)
on 2006-04-18 15:55
(Received via mailing list)
Hi everyone, I need a little support. I have this script:

outfile = ARGV.shift

lines = ARGF.readlines
marked_up_lines = lines.map do |line|
  words = line.split
  '<mezo eazon="' + words[0] + '">' + words[1] + '</mezo>' + "\n"
end

File.open(outfile,'w') do |file|
  file.write marked_up_lines.join
end

This should split words in two columns to a HTML tag. But if I have more
then two words in the columns it cut down from the thrid word.
What should I add to see the other words? Or, does anyone have a better
script?
Regards,

Daniel
Farrel L. (Guest)
on 2006-04-18 16:04
(Received via mailing list)
You could use shift and then join the rest:

"<mezo eazon=\"#{words.shift}\">#{words.join(' ')}</mezo>\n"
Paul B. (Guest)
on 2006-04-18 16:54
(Received via mailing list)
On 18/04/06, Farrel L. <removed_email_address@domain.invalid> wrote:
> You could use shift and then join the rest:

Even better, just don't split them in the first place:

marked_up_lines = lines.map do |line|
  eazon, mezo = line.split($;, 2)
  "<mezo eazon=\"#{eazon}\">#{mezo}</mezo>\n"
end

Paul.
This topic is locked and can not be replied to.