Read individual words from input?

Hello folks,

I'm a semi-newbie C++ programmer and a total newbie Ruby programmer, so
please bear with my incompetence.

In C++, as I'm sure most of you know, it's possible to read individual
"words" into string variables by using the stream extraction operator.
This operator automatically excludes any and all whitespace from the
value it places into the string variable--that is, in the process of
reading the next piece of input, it skips any leading whitespace, and
stops upon encountering any following whitespace.

I'm struggling to find an equivalent to this in Ruby. I'd like to open a
text file and create from it an array each of whose values is an
individual "word," sans whitespace.

For example, given a string along the lines of " \n\tHello \n \t My
\t\t\t\nName \t\n Is \n John", how can I cleanly extract the strings
"Hello", "My", "Name", "Is", and "John"?

Again, sorry for asking such a stupid question--just trying to get a
handle on what seems to be an awesome language.

Thanks for any help,
Keriostar

···

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

Hi,

Use String#split, which by default splits on whitespace. It returns the
substrings in an Array:

irb(main):001:0> " \n\tHello \n \t My \t\t\t\nName \t\n Is \n
John".split
=> ["Hello", "My", "Name", "Is", "John"]

Regards,
R.

···

2007/6/23, Kerio Star <keriostar@gmail.com>:

Hello folks,

I'm a semi-newbie C++ programmer and a total newbie Ruby programmer, so
please bear with my incompetence.

In C++, as I'm sure most of you know, it's possible to read individual
"words" into string variables by using the stream extraction operator.
This operator automatically excludes any and all whitespace from the
value it places into the string variable--that is, in the process of
reading the next piece of input, it skips any leading whitespace, and
stops upon encountering any following whitespace.

I'm struggling to find an equivalent to this in Ruby. I'd like to open a
text file and create from it an array each of whose values is an
individual "word," sans whitespace.

For example, given a string along the lines of " \n\tHello \n \t My
\t\t\t\nName \t\n Is \n John", how can I cleanly extract the strings
"Hello", "My", "Name", "Is", and "John"?

Again, sorry for asking such a stupid question--just trying to get a
handle on what seems to be an awesome language.

Thanks for any help,
Keriostar

--
Posted via http://www.ruby-forum.com/\.

Hi. Without any file checking for existing files etc. You can use the read
method of file. By calling this in a block it also closes the file for you.

words = File.open( "404.html" ){ |f| f.read.split }

···

On 6/23/07, Kerio Star <keriostar@gmail.com> wrote:

Hello folks,

I'm a semi-newbie C++ programmer and a total newbie Ruby programmer, so
please bear with my incompetence.

In C++, as I'm sure most of you know, it's possible to read individual
"words" into string variables by using the stream extraction operator.
This operator automatically excludes any and all whitespace from the
value it places into the string variable--that is, in the process of
reading the next piece of input, it skips any leading whitespace, and
stops upon encountering any following whitespace.

I'm struggling to find an equivalent to this in Ruby. I'd like to open a
text file and create from it an array each of whose values is an
individual "word," sans whitespace.

For example, given a string along the lines of " \n\tHello \n \t My
\t\t\t\nName \t\n Is \n John", how can I cleanly extract the strings
"Hello", "My", "Name", "Is", and "John"?

Again, sorry for asking such a stupid question--just trying to get a
handle on what seems to be an awesome language.

Thanks for any help,
Keriostar

Kerio Star wrote:

Hello folks,

I'm a semi-newbie C++ programmer and a total newbie Ruby programmer, so
please bear with my incompetence.

In C++, as I'm sure most of you know, it's possible to read individual
"words" into string variables by using the stream extraction operator.
This operator automatically excludes any and all whitespace from the
value it places into the string variable--that is, in the process of
reading the next piece of input, it skips any leading whitespace, and
stops upon encountering any following whitespace.

I'm struggling to find an equivalent to this in Ruby. I'd like to open a
text file and create from it an array each of whose values is an
individual "word," sans whitespace.

For example, given a string along the lines of " \n\tHello \n \t My
\t\t\t\nName \t\n Is \n John", how can I cleanly extract the strings
"Hello", "My", "Name", "Is", and "John"?

Again, sorry for asking such a stupid question--just trying to get a
handle on what seems to be an awesome language.

Thanks for any help,
Keriostar

Alternative if you want to avoid having interpunctuation and other stuff
in your words:
words = string.scan(/\w+/)

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Sat, 23 Jun 2007, Daniel N wrote:

On 6/23/07, Kerio Star <keriostar@gmail.com> wrote:

Hello folks,

I'm a semi-newbie C++ programmer and a total newbie Ruby programmer, so
please bear with my incompetence.

In C++, as I'm sure most of you know, it's possible to read individual
"words" into string variables by using the stream extraction operator.
This operator automatically excludes any and all whitespace from the
value it places into the string variable--that is, in the process of
reading the next piece of input, it skips any leading whitespace, and
stops upon encountering any following whitespace.

I'm struggling to find an equivalent to this in Ruby. I'd like to open a
text file and create from it an array each of whose values is an
individual "word," sans whitespace.

For example, given a string along the lines of " \n\tHello \n \t My
\t\t\t\nName \t\n Is \n John", how can I cleanly extract the strings
"Hello", "My", "Name", "Is", and "John"?

Again, sorry for asking such a stupid question--just trying to get a
handle on what seems to be an awesome language.

Thanks for any help,
Keriostar

Hi. Without any file checking for existing files etc. You can use the read
method of file. By calling this in a block it also closes the file for you.

words = File.open( "404.html" ){ |f| f.read.split }

You can also use the class method:

   words = File.read("words.txt").split

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)