PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Newbie: what's Ruby idiom for word-by-word input?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Newbie: what's Ruby idiom for word-by-word input?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 20h08   #1 (permalink)
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Newbie: what's Ruby idiom for word-by-word input?

Hi,

What is the Ruby idiom for reading input word-by-word? In other words
-- how to process input while skipping all of the whitespace.

I would use this code in C++:

//std::istream& stream;
while (!stream.eof())
{
std::string str;
stream >> str;
...
}

Or something like this in C:

char str[100]; // please no flame on fixed buffer size :-)
while (!feof(file))
{
fscanf("%99s", str);
...
}

I have tried to use Ruby's scanf("%s") but found it severely broken
(as per my task)--it discards the rest of the input up to the
newline. I currently use the following approach, which I find ugly:

while not $stdin.eof? do
words = $stdin.gets().scan(/[^\s]+/)
words.each do |w|
...
end
end

It takes me to write an inner loop and reads the entire string into
memory and then splits it into array of words... Duh!

My application is not in any case a time- or memory-critical, nor did
I measured to find the performance bottleneck... however, I desire for
the enlightenment. :-)

Please show me the Ruby way!


Cheers,
Alex
PS: Yes, I've searched the web, tutorials, FAQs, cookbooks, etc.
before posting this. No luck.

  Réponse avec citation
Vieux 16/09/2007, 20h19   #2 (permalink)
Ari Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?


On Sep 16, 2007, at 3:10 PM, Alex Shulgin wrote:

> Please show me the Ruby way!
>


http://ec1.images-amazon.com/images/I/41X2833B8TL.jpg

I believe that should be enough to show you the ruby way.

Maybe you could try this:

a = gets.chomp #=> "My name is Ari"
words = a.split(/ /) #=> ["My", "name", "is", "Ari"]

Tadah! I REALLY hope that's what you're looking for.

-------------------------------------------------------|
~ Ari
crap my sig won't fit


  Réponse avec citation
Vieux 16/09/2007, 22h19   #3 (permalink)
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

On 16.09.2007 21:08, Alex Shulgin wrote:
> Hi,
>
> What is the Ruby idiom for reading input word-by-word? In other words
> -- how to process input while skipping all of the whitespace.
>
> I would use this code in C++:
>
> //std::istream& stream;
> while (!stream.eof())
> {
> std::string str;
> stream >> str;
> ...
> }
>
> Or something like this in C:
>
> char str[100]; // please no flame on fixed buffer size :-)
> while (!feof(file))
> {
> fscanf("%99s", str);
> ...
> }
>
> I have tried to use Ruby's scanf("%s") but found it severely broken
> (as per my task)--it discards the rest of the input up to the
> newline. I currently use the following approach, which I find ugly:
>
> while not $stdin.eof? do
> words = $stdin.gets().scan(/[^\s]+/)
> words.each do |w|
> ...
> end
> end
>
> It takes me to write an inner loop and reads the entire string into
> memory and then splits it into array of words... Duh!
>
> My application is not in any case a time- or memory-critical, nor did
> I measured to find the performance bottleneck... however, I desire for
> the enlightenment. :-)
>
> Please show me the Ruby way!
>
>
> Cheers,
> Alex
> PS: Yes, I've searched the web, tutorials, FAQs, cookbooks, etc.
> before posting this. No luck.


If you do not need to treat every word before it is read from the input
you could do this:

$stdin.each do |line|
line.scan /\w+/ do |word|
puts word
end
end

If your definition of "word" is different (i.e. non whitespace
characters) you need a different regexp (for example /\S+/).

If you want to read to word boundaries only it becomes more difficult.

Kind regards

robert
  Réponse avec citation
Vieux 17/09/2007, 10h30   #4 (permalink)
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

On Sep 17, 12:19 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>
> If you do not need to treat every word before it is read from the input
> you could do this:
>
> $stdin.each do |line|
> line.scan /\w+/ do |word|
> puts word
> end
> end
>
> If your definition of "word" is different (i.e. non whitespace
> characters) you need a different regexp (for example /\S+/).
>
> If you want to read to word boundaries only it becomes more difficult.


This is more or less the same code as I use, maybe a bit more
readable, tough. :-)

So there is no way in Ruby to read words w/o reading the whole line of
input and then splitting/scanning the line (which takes us two nested
loops anyway)? Looks very odd to me...


Alex

  Réponse avec citation
Vieux 17/09/2007, 14h47   #5 (permalink)
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

2007/9/17, Alex Shulgin <alex.shulgin@gmail.com>:
> On Sep 17, 12:19 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> >
> > If you do not need to treat every word before it is read from the input
> > you could do this:
> >
> > $stdin.each do |line|
> > line.scan /\w+/ do |word|
> > puts word
> > end
> > end
> >
> > If your definition of "word" is different (i.e. non whitespace
> > characters) you need a different regexp (for example /\S+/).
> >
> > If you want to read to word boundaries only it becomes more difficult.

>
> This is more or less the same code as I use, maybe a bit more
> readable, tough. :-)
>
> So there is no way in Ruby to read words w/o reading the whole line of
> input and then splitting/scanning the line (which takes us two nested
> loops anyway)? Looks very odd to me...


Well, you can use #getc and implement the word matching logic
yourself. But that is more tedious and it's also questionable whether
that will be as efficient. And since a line break is a word boundary
anyway the nested loop approach yields the proper result (aka sequence
of words) as the other approach. So why bother to create a word
iterating solution just to get rid of one level of loop nesting?

Kind regards

robert

  Réponse avec citation
Vieux 17/09/2007, 16h19   #6 (permalink)
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

On Sep 17, 4:30 am, Alex Shulgin <alex.shul...@gmail.com> wrote:
> On Sep 17, 12:19 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>
>
>
> > If you do not need to treat every word before it is read from the input
> > you could do this:

>
> > $stdin.each do |line|
> > line.scan /\w+/ do |word|
> > puts word
> > end
> > end

>
> > If your definition of "word" is different (i.e. non whitespace
> > characters) you need a different regexp (for example /\S+/).

>
> > If you want to read to word boundaries only it becomes more difficult.

>
> This is more or less the same code as I use, maybe a bit more
> readable, tough. :-)
>
> So there is no way in Ruby to read words w/o reading the whole line of
> input and then splitting/scanning the line (which takes us two nested
> loops anyway)? Looks very odd to me...
>
> Alex


Awk is a very popular tool for text processing, but there is no
way to make it treat a sequence of whitespace characters as a
record-separator. So in awk, as in Ruby, text is almost always
read a line at a time.
Gawk added the ability to set the record-separator to a
regular expression:

gawk 'BEGIN{RS="[ \t\n]+"} 1'

  Réponse avec citation
Vieux 17/09/2007, 19h00   #7 (permalink)
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

On Sep 17, 6:19 pm, William James <w_a_x_...@yahoo.com> wrote:
>
> Awk is a very popular tool for text processing, but there is no
> way to make it treat a sequence of whitespace characters as a
> record-separator. So in awk, as in Ruby, text is almost always
> read a line at a time.


I thought Ruby is not just a text processing tool, but a general
purpose programming language. Anyway, it would be nice to have a
solution for this problem as compact and flexible as C++ example I've
provided. What if scanf() didn't discard the rest of the line... but
now is too late to fix it. :-/


Regards,
Alex

  Réponse avec citation
Vieux 17/09/2007, 19h31   #8 (permalink)
Bertram Scharpf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

Hi,

Am Montag, 17. Sep 2007, 04:19:53 +0900 schrieb Ari Brown:
> On Sep 16, 2007, at 3:10 PM, Alex Shulgin wrote:
>> Please show me the Ruby way!

>
> Maybe you could try this:
>
> a = gets.chomp #=> "My name is Ari"
> words = a.split(/ /) #=> ["My", "name", "is", "Ari"]


This isn't actually elaborate as it doesn't recognize tabs
or multiple whitespace. It is even longer than

words = gets.split

With no argument or nil, String#split uses $; what normally
is nil, too. Then, split uses something like %r/[ \t\n\r\v\f]+/.

You may easily read whole lines; they shouldn't become too
long. Reading the first word before the user typed enter
would need to tweak terminal settings. Not worth the effort
in most cases.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h33.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14055 seconds with 16 queries