[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

{newb} file and puts

STEPHEN BECKER I V

10/12/2004 7:46:00 PM

f="z:\t.txt"
loc = File.open(f, "w+")
loc.puts((i+65).chr,' ',a[i]) if a[i]>0


Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
the same line in the file? i get
A

30
B

23
C

43
D

28
E

47
and i want
A 30
B 23
C 43
D 28
E 47

Becker


7 Answers

James Gray

10/12/2004 7:53:00 PM

0

On Oct 12, 2004, at 2:45 PM, STEPHEN BECKER I V wrote:

> f="z:\t.txt"
> loc = File.open(f, "w+")
> loc.puts((i+65).chr,' ',a[i]) if a[i]>0
>
>
> Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
> the same line in the file?

Use print() instead of puts() and insert \n characters as needed.

James Edward Gray II



Carlos Tirado

10/12/2004 7:57:00 PM

0

change last line:

loc.puts((i+65).chr+' '+a[i].to_s) if a[i]>0


C


From: STEPHEN BECKER I V <Becker004@gmail.com>
Date: Wed, 13 Oct 2004 04:45:34 +0900

>f="z:\t.txt"
>loc = File.open(f, "w+")
>loc.puts((i+65).chr,' ',a[i]) if a[i]>0
>
>
>Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
>the same line in the file? i get
>A
>
>30
>B
>
>23
>C
>
>43
>D
>
>28
>E
>
>47
>and i want
>A 30
>B 23
>C 43
>D 28
>E 47
>
>Becker
>



Jason Voegele

10/12/2004 7:59:00 PM

0

James Edward Gray II said:
> On Oct 12, 2004, at 2:45 PM, STEPHEN BECKER I V wrote:
>
>> f="z:\t.txt"
>> loc = File.open(f, "w+")
>> loc.puts((i+65).chr,' ',a[i]) if a[i]>0
>>
>>
>> Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
>> the same line in the file?
>
> Use print() instead of puts() and insert \n characters as needed.

Alternatively, continue to use puts() and use string variable
interpolation instead of separate parameters. In other words:

loc.puts("#{(i+65).chr} #{a[i]}") if a[i]>0

--
Jason Voegele
"There is an essential core at the center of each man and woman that
remains unaltered no matter how life's externals may be transformed
or recombined. But it's smaller than we think."
-- Gene Wolfe, The Book of the Long Sun





STEPHEN BECKER I V

10/12/2004 8:02:00 PM

0

Thank you so much! That kinda sucks when you need to output an int,
you need to remember the to_s
Thank you again
Becker


On Wed, 13 Oct 2004 04:56:38 +0900, Carlos Tirado
<li_rubytalk@tremendo.com> wrote:
> change last line:
>
> loc.puts((i+65).chr+' '+a[i].to_s) if a[i]>0
>
> C
>
> From: STEPHEN BECKER I V <Becker004@gmail.com>
> Date: Wed, 13 Oct 2004 04:45:34 +0900
>
>
>
> >f="z:\t.txt"
> >loc = File.open(f, "w+")
> >loc.puts((i+65).chr,' ',a[i]) if a[i]>0
> >
> >
> >Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
> >the same line in the file? i get
> >A
> >
> >30
> >B
> >
> >23
> >C
> >
> >43
> >D
> >
> >28
> >E
> >
> >47
> >and i want
> >A 30
> >B 23
> >C 43
> >D 28
> >E 47
> >
> >Becker
> >
>
>


STEPHEN BECKER I V

10/12/2004 8:03:00 PM

0

Yes, i see there is always 6 ways to do the same thing... i think i
will be using print from now on.
Becker


On Wed, 13 Oct 2004 04:52:57 +0900, James Edward Gray II
<james@grayproductions.net> wrote:
> On Oct 12, 2004, at 2:45 PM, STEPHEN BECKER I V wrote:
>
> > f="z:\t.txt"
> > loc = File.open(f, "w+")
> > loc.puts((i+65).chr,' ',a[i]) if a[i]>0
> >
> >
> > Nevermind what it is writing, How do i keep the (i+65).chr and a[i] on
> > the same line in the file?
>
> Use print() instead of puts() and insert \n characters as needed.
>
> James Edward Gray II
>
>


James Gray

10/12/2004 8:07:00 PM

0

On Oct 12, 2004, at 3:01 PM, STEPHEN BECKER I V wrote:

> Thank you so much! That kinda sucks when you need to output an int,
> you need to remember the to_s

You don't. Just use the interpolation technique that was suggested
with either puts() or print().

Hope that helps.

James Edward Gray II



Ara.T.Howard

10/12/2004 8:25:00 PM

0