Nuby question - printing substring as non-ASCII

T

Thomas Luedeke

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
 
D

David Vallner

--------------enig9F080F22A5592FC8DC73EBBB
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Thomas said:
I'm reading in a string of numbers, as follows:
=20
" 1 45.3456 "
=20
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=20
work on a substring.
=20

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
}
=3D> [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


--------------enig9F080F22A5592FC8DC73EBBB
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFXcoNy6MhrS8astoRAq3QAJ9v6HUVmcq52eYSLBw5LH4PV+YJNgCeJdxM
Hw5oaQdLaXXh4h+89kfq2Ws=
=WH/P
-----END PGP SIGNATURE-----

--------------enig9F080F22A5592FC8DC73EBBB--
 
D

Dmitry Buzdin

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
 
P

Patrick Spence

Thomas said:
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

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

Patrick Spence

Dmitry said:
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 :eek:|
 
T

Thomas Luedeke

Thomas said:
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"
 
W

Wilson Bilkovich

Thomas said:
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]
 
D

dblack

Hi --

Thomas said:
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]

Another possibility:


irb(main):001:0> require 'scanf'
=> true
irb(main):002:0> string = " 1 45.3456 "
=> " 1 45.3456 "
irb(main):003:0> string.scanf("%d")
=> [1]

You have to get it out of the array, but you don't have to convert it
(it's already an integer).


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top