[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

extra text in puts command

Jenny Purcell

7/22/2007 5:43:00 AM

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
2 Answers

David and Sharon Phillips

7/22/2007 6:14:00 AM

0

On 22/07/2007, at 3:45 PM, Jenny Purcell wrote:

> 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


Jenny Purcell

7/22/2007 4:12:00 PM

0

On Sun, 22 Jul 2007 15:14:00 +0900, Sharon Phillips
<phillipsds@yahoo.co.uk> wrote:

>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