[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Formatting text.

Marcin Tyman

7/25/2007 9:33:00 AM

Hi again :-)
Is there any way to format a string. I.E. I've got a variable which may
be 1, 2, 3 and the variable is added to string. I want to show whole
string formatted as follows:
100
1
10
1100

not like that:
100
1
10
1100

Any help would be helpful (as always ;-) )

thanks in advance
--
Posted via http://www.ruby-....

6 Answers

Robert Klemme

7/25/2007 9:38:00 AM

0

2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
> Hi again :-)
> Is there any way to format a string. I.E. I've got a variable which may
> be 1, 2, 3 and the variable is added to string. I want to show whole
> string formatted as follows:
> 100
> 1
> 10
> 1100
>
> not like that:
> 100
> 1
> 10
> 1100
>
> Any help would be helpful (as always ;-) )

Please look for printf, sprintf and % in the docs. The last one might
be hard to find. You use it as: "%3d" % 1

Kind regards

robert

Marcin Tyman

7/25/2007 9:49:00 AM

0

Robert Klemme wrote:
> 2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
>> 100
>> 1
>> 10
>> 1100
>>
>> Any help would be helpful (as always ;-) )
>
> Please look for printf, sprintf and % in the docs. The last one might
> be hard to find. You use it as: "%3d" % 1
>
> Kind regards
>
> robert

OK, Fine. But... I want to format the string not its output to redirect
the string to a file on remote machine (the file should be formatted).
--
Posted via http://www.ruby-....

Sebastian Hungerecker

7/25/2007 9:54:00 AM

0

Marcin Tyman wrote:
> Robert Klemme wrote:

> > Please look for printf, sprintf and % in the docs. The last one might
> > be hard to find. You use it as: "%3d" % 1
>
> OK, Fine. But... I want to format the string not its output to redirect
> the string to a file on remote machine (the file should be formatted).

sprintf and % return a string, they don't print anything to the screen. You
can write the result of "%3d" % 1 to a file without a problem.


--
NP: Katatonia - Fractured
Ist so, weil ist so
Bleibt so, weil war so

Robert Klemme

7/25/2007 9:55:00 AM

0

2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
> Robert Klemme wrote:
> > 2007/7/25, Marcin Tyman <m.tyman@interia.pl>:
> >> 100
> >> 1
> >> 10
> >> 1100
> >>
> >> Any help would be helpful (as always ;-) )
> >
> > Please look for printf, sprintf and % in the docs. The last one might
> > be hard to find. You use it as: "%3d" % 1
> >
> > Kind regards
> >
> > robert
>
> OK, Fine. But... I want to format the string not its output to redirect
> the string to a file on remote machine (the file should be formatted).

Now what? Do you want to output or not? I do not understand your
remark because with the methods I listed you can do output as well as
change strings.

robert

Peña, Botp

7/25/2007 10:03:00 AM

0

From: Marcin Tyman [mailto:m.tyman@interia.pl]
# OK, Fine. But... I want to format the string not its output
# to redirect the string to a file on remote machine (the file
# should be formatted).

the simple way is just do the output on screen (in a format you want). then if you're happy with the output, just redirect it to a file.

like eg,

ruby mytestprogram.rb > myfile

just try it.

kind regards -botp

Kaldrenon

7/25/2007 1:03:00 PM

0

On Jul 25, 5:54 am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> sprintf and % return a string, they don't print anything to the screen. You
> can write the result of "%3d" % 1 to a file without a problem.

Same is true for rjust() : http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html#St...