[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Nuby question - printing substring as non-ASCII

Thomas Luedeke

11/17/2006 2:35:00 PM

I'm reading in a string of numbers, as follows:

" 1 45.3456 "

How do I write out the first non-whitespace element as a number? Ruby
wants to express as it in ASCII, and the .to_i method doesn't seem to
work on a substring.

Thanks in advance.

TPL

--
Posted via http://www.ruby-....

8 Answers

David Vallner

11/17/2006 2:41:00 PM

0

Thomas Luedeke wrote:
> I'm reading in a string of numbers, as follows:
>
> " 1 45.3456 "
>
> How do I write out the first non-whitespace element as a number? Ruby
> wants to express as it in ASCII, and the .to_i method doesn't seem to
> work on a substring.
>

Doesn't seem to? Code please.

Also, the following works for me:

irb(main):009:0> " 1 45.3456 ".strip.split(/\s+/).map { |i|
i.to_i
}
=> [1, 45]

(Of course, to_i coerces to integers, so the body of the map block would
have to be more complicated to get floats too.)


David Vallner

Dmitry Buzdin

11/17/2006 2:44:00 PM

0

Then why not call to_i on a whole string?

" 1 45.3456 ".to_i
" 1 45.3456 ".to_f

It will find the first number and convert it to Fixnum or Float.

Dmitry

Thomas Luedeke wrote:
> I'm reading in a string of numbers, as follows:
>
> " 1 45.3456 "
>
> How do I write out the first non-whitespace element as a number? Ruby
> wants to express as it in ASCII, and the .to_i method doesn't seem to
> work on a substring.
>
> Thanks in advance.
>
> TPL
>
> --
> Posted via http://www.ruby-....

Patrick Spence

11/17/2006 2:45:00 PM

0


Thomas Luedeke wrote:
> I'm reading in a string of numbers, as follows:
>
> " 1 45.3456 "
>
> How do I write out the first non-whitespace element as a number? Ruby
> wants to express as it in ASCII, and the .to_i method doesn't seem to
> work on a substring.
>
> Thanks in advance.
>
> TPL
>
> --
> Posted via http://www.ruby-....

irb(main):001:0> " 1 45.3456 ".strip.to_i()
=> 1


Patrick Spence

11/17/2006 2:55:00 PM

0


Dmitry Buzdin wrote:
> Then why not call to_i on a whole string?
>
> " 1 45.3456 ".to_i
> " 1 45.3456 ".to_f
>
> It will find the first number and convert it to Fixnum or Float.
>
> Dmitry
You're absolutely right, Dmitry... .strip() is not necessary. But I
tend to be a bit anal-retentive about such things :o|


Thomas Luedeke

11/17/2006 3:28:00 PM

0

Thomas Luedeke wrote:
> I'm reading in a string of numbers, as follows:
>
> " 1 45.3456 "
>
> How do I write out the first non-whitespace element as a number? Ruby
> wants to express as it in ASCII, and the .to_i method doesn't seem to
> work on a substring.
>
> Thanks in advance.
>
> TPL


Thanks. Dumb question, but I'm kinda deep in the throes of the "suck"
stage of learning Ruby. The following also seems to work (at least for
the purposes of reading in and writing formatted output):

irb(main):001:0> string=" 1 45.3456 "
=> " 1 45.3456 "

irb(main):003:0> string.strip
=> "1 45.3456"

irb(main):004:0> string.split[1]
=> "45.3456"

irb(main):005:0> string.split[0]
=> "1"

--
Posted via http://www.ruby-....

Wilson Bilkovich

11/17/2006 4:14:00 PM

0

On 11/17/06, Thomas Luedeke <tluedeke@excite.com> wrote:
> Thomas Luedeke wrote:
> > I'm reading in a string of numbers, as follows:
> >
> > " 1 45.3456 "
> >
> > How do I write out the first non-whitespace element as a number? Ruby
> > wants to express as it in ASCII, and the .to_i method doesn't seem to
> > work on a substring.
> >
> > Thanks in advance.
> >
> > TPL
>
>
> Thanks. Dumb question, but I'm kinda deep in the throes of the "suck"
> stage of learning Ruby. The following also seems to work (at least for
> the purposes of reading in and writing formatted output):
>
> irb(main):001:0> string=" 1 45.3456 "
> => " 1 45.3456 "
>
> irb(main):003:0> string.strip
> => "1 45.3456"
>
> irb(main):004:0> string.split[1]
> => "45.3456"
>
> irb(main):005:0> string.split[0]
> => "1"
>

Here is one way:
irb(main):001:0> string = " 1 45.3456 "
=> " 1 45.3456 "
irb(main):002:0> substrings = string.scan /[^\s]+/
=> ["1", "45.3456"]
irb(main):003:0> substrings.first.to_i
=> 1

The second line of code is saying: Scan the string, and return an
Array of strings that were made up of one or more non-whitespace
characters.

substrings.first is the same as substrings[0]

dblack

11/17/2006 5:02:00 PM

0

Bernard Kenik

11/18/2006 5:24:00 PM

0


----- Original Message -----
From: "Patrick Spence" <djmj12@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Friday, November 17, 2006 9:45 AM
Subject: Re: Nuby question - printing substring as non-ASCII


>
> Thomas Luedeke wrote:
>> I'm reading in a string of numbers, as follows:
>>
>> " 1 45.3456 "
>>
>> How do I write out the first non-whitespace element as a number? Ruby
>> wants to express as it in ASCII, and the .to_i method doesn't seem to
>> work on a substring.
>>
>> Thanks in advance.
>>
>> TPL
>>
>> --
>> Posted via http://www.ruby-....
>
> irb(main):001:0> " 1 45.3456 ".strip.to_i()
> => 1
>
no need to strip
" 1 45.3456".to_i
=> 1