extra text in puts command

J

Jenny Purcell

This one seems incredibly simple and I'm sure I'm just missing
something obvious.

I have the following 'puts' command to push my results to a csv file:

puts CSV.generate_line([pole_new[j], horse_name[j], horse_info[j],
owners_initials[j], best_result[j], race_style[j], best_time[j],
pre_points[j], roll_1[j], roll_2[j], roll_3[j], roll_4[j], roll_5[j],
score[j], position[j]])


What I'd like to do is add "pp" in front of the entry pole_new[j].

So, the results in the csv file will look like "pp6" when the contents
of pole_new is "6"

For position, I'd like to do something similar, but, instead I'd like
to it come out as "<position[j]>/<count>". So, I want to combine
position[j] and count in one CSV field. If the position is first of
ten, it would look like "1/10" in the CSV file.

Do I need to escape the slash "/" and "pp" in some way?

Jenny
 
S

Sharon Phillips

This one seems incredibly simple and I'm sure I'm just missing
something obvious.

I have the following 'puts' command to push my results to a csv file:

puts CSV.generate_line([pole_new[j], horse_name[j], horse_info[j],
owners_initials[j], best_result[j], race_style[j], best_time[j],
pre_points[j], roll_1[j], roll_2[j], roll_3[j], roll_4[j], roll_5[j],
score[j], position[j]])


What I'd like to do is add "pp" in front of the entry pole_new[j].

So, the results in the csv file will look like "pp6" when the contents
of pole_new is "6"

For position, I'd like to do something similar, but, instead I'd like
to it come out as "<position[j]>/<count>". So, I want to combine
position[j] and count in one CSV field. If the position is first of
ten, it would look like "1/10" in the CSV file.

Do I need to escape the slash "/" and "pp" in some way?

Jenny

Hi Jenny,

you could try
puts CSV.generate_line(["pp#{pole_new[j]}", horse_name[j], ... , "#
{position[j]}/#{count}" ] )

By using double quotes, Ruby checks the string first for special
sequences which get evaluated first and the results inserted into the
string. To 'drop in' a value we can surround it with #{ and }.
Example
puts "2 + 3 = #{2+3}"
or
puts "pp#{pole_new[j]}"
In these examples, the expression between #{ and } is evaluated first
and the result substituted, so #{2+3} is replaced with the result, 5.
'/' does not require escaping, but '\' does. To escape '\' just put
another before it "\\"

Note that using single quotes makes Ruby take the string 'as is'. It
ignores any escape chars and #{...} expressions.
puts "2 + 3 = #{2+3}" # double quotes
2 + 3 = 5
puts '2 + 3 = #{2+3}' # single quotes
2 + 3 = #{2+3}

I hope this makes sense...

Cheers,
Dave
 
J

Jenny Purcell

On 22/07/2007, at 3:45 PM, Jenny Purcell wrote:
Do I need to escape the slash "/" and "pp" in some way?

Jenny

Hi Jenny,

you could try
puts CSV.generate_line(["pp#{pole_new[j]}", horse_name[j], ... , "#
{position[j]}/#{count}" ] )

By using double quotes, Ruby checks the string first for special
sequences which get evaluated first and the results inserted into the
string. To 'drop in' a value we can surround it with #{ and }.
Example
puts "2 + 3 = #{2+3}"
or
puts "pp#{pole_new[j]}"
In these examples, the expression between #{ and } is evaluated first
and the result substituted, so #{2+3} is replaced with the result, 5.
'/' does not require escaping, but '\' does. To escape '\' just put
another before it "\\"

Note that using single quotes makes Ruby take the string 'as is'. It
ignores any escape chars and #{...} expressions.
puts "2 + 3 = #{2+3}" # double quotes
2 + 3 = 5
puts '2 + 3 = #{2+3}' # single quotes
2 + 3 = #{2+3}

I hope this makes sense...

Cheers,
Dave
Thank for the help, it worked very well, although I did decide to
change it to look like 1(12) instead of 1/12 (it was treating the
latter as a date). But, the quotes and the pound sign worked like a
charm!

Also, I looked over where you converted my code over to OO, and I can
sort of follow what's happening, but, not well enough to expand the
program any further (it's probably only about half written). So, I'll
keep what you've done as a reference, but, I'm going to continue to
write it in a procedural style, until it's "done-done". Then I can
decide if I have the extra time to invest in trying to grok OO style.

Jenny
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top