Strings .each method

C

Chris Hickman

I am using .each to pull apart a string at every space. Is there a way
to peek ahead to see what the next substring is without moving to the
next substring in the block?

I have a ruby book and I was searching for this through google but have
had no luck so far. Didn't know if this was an easy answer for anybody.


Thanks in advance,

Chris
 
L

Lloyd Linklater

Chris said:
I am using .each to pull apart a string at every space. Is there a way
to peek ahead to see what the next substring is without moving to the
next substring in the block?

I have a ruby book and I was searching for this through google but have
had no luck so far. Didn't know if this was an easy answer for anybody.

I am not sure what you mean but here is how to perform the task:

a = "This is a big old string."
p a.split

=> ["This", "is", "a", "big", "old", "string."]
 
C

Chris Hickman

I am not sure what you mean but here is how to perform the task:

I have something like the following. I tried to make it a little
simpler.

a = "This is a big old string."

a.each do |w|

**I have a bunch of if else statements in this block**

end


So w = "This" first time around
w = "is" second time around
w = "a" third time around ...

What I was trying to ask is when w = "big" inside this block is there a
way I could peek ahead within the block and see "old" as the next
substring without assigning "old" to w?

Does that make more sense?

Thanks a lot for taking a look,

Chris
 
C

Corey Haines

[Note: parts of this message were removed to make it a legal post.]

I'm not sure about looking ahead to the next element using #each, but if you
can describe why you want to do it, there might be a different way than
using #each.

-Corey
 
C

Chris Hickman

Corey said:
but if you can describe why you want to do it, there might be a different way

It is kind of hard to explain but I have a long string that someone
typed into a form which was then saved into a database. They used made
up tags such as "<choose1>, </choose1>, <row>, </row>, <tbl> ..." I
have to translate the string into html and add javascript etc. based on
the different tags they wrote.

I have it working for everything except one task. In this one task I
want to pass the words between two tags as a value for the javascript.
The problem is it is pulling the phrase apart at every space. I know I
can add .each(???) and have it pull everything apart at the ??? but it
would involve a lot of adding ??? to this long string.

What I am trying to do is add a while loop inside the block that will
look ahead at the next substring to see if it is a "</choose1>". If it
is I want it to exit the while loop and go on without throwing away the
"</choose1>" tag because it is needed next time through the block.

Sorry if that didnt make sense at all.
 
R

Rob Biedenharn

It is kind of hard to explain but I have a long string that someone
typed into a form which was then saved into a database. They used
made
up tags such as "<choose1>, </choose1>, <row>, </row>, <tbl> ..." I
have to translate the string into html and add javascript etc. based
on
the different tags they wrote.

I have it working for everything except one task. In this one task I
want to pass the words between two tags as a value for the javascript.
The problem is it is pulling the phrase apart at every space. I
know I
can add .each(???) and have it pull everything apart at the ??? but it
would involve a lot of adding ??? to this long string.

What I am trying to do is add a while loop inside the block that will
look ahead at the next substring to see if it is a "</choose1>". If
it
is I want it to exit the while loop and go on without throwing away
the
"</choose1>" tag because it is needed next time through the block.

Sorry if that didnt make sense at all.

Perhaps something like this:

irb> require 'enumerator'
=> true
irb> a = "This is a big list of words"
=> "This is a big list of words"
irb> (a.split(' ') << nil).each_cons(2) {|word,lookahead| puts "%-10s
%s"%[word.inspect,lookahead.inspect]}
"This" "is"
"is" "a"
"a" "big"
"big" "list"
"list" "of"
"of" "words"
"words" nil
=> nil

Putting the << nil gives the last word something to be spit out of the
each_cons with.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
C

Chris Hickman

irb> (a.split(' ') << nil).each_cons(2) {|word,lookahead| puts "%-10s
%s"%[word.inspect,lookahead.inspect]}


Awesome. I understand how to use it but what exactly does %-10s %s"%
mean/do?


Thanks a lot for the feedback,

chris
 
R

Rob Biedenharn

irb> (a.split(' ') << nil).each_cons(2) {|word,lookahead| puts "%-10s
%s"%[word.inspect,lookahead.inspect]}

Awesome. I understand how to use it but what exactly does %-10s %s"%
mean/do?


Thanks a lot for the feedback,

chris


That's a way to do formatted output like Kernel.sprintf, but shorter
to type using String#%

%-10s means a string(s) left-justified(-) in a 10 characters wide
space (will use more than 10 if the string to be formatted is longer)

%s means a whole string

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
C

Corey Haines

[Note: parts of this message were removed to make it a legal post.]

Very cool, Rob!

It is kind of hard to explain but I have a long string that someone
typed into a form which was then saved into a database. They used
made
up tags such as "<choose1>, </choose1>, <row>, </row>, <tbl> ..." I
have to translate the string into html and add javascript etc. based
on
the different tags they wrote.

I have it working for everything except one task. In this one task I
want to pass the words between two tags as a value for the javascript.
The problem is it is pulling the phrase apart at every space. I
know I
can add .each(???) and have it pull everything apart at the ??? but it
would involve a lot of adding ??? to this long string.

What I am trying to do is add a while loop inside the block that will
look ahead at the next substring to see if it is a "</choose1>". If
it
is I want it to exit the while loop and go on without throwing away
the
"</choose1>" tag because it is needed next time through the block.

Sorry if that didnt make sense at all.

Perhaps something like this:

irb> require 'enumerator'
=> true
irb> a = "This is a big list of words"
=> "This is a big list of words"
irb> (a.split(' ') << nil).each_cons(2) {|word,lookahead| puts "%-10s
%s"%[word.inspect,lookahead.inspect]}
"This" "is"
"is" "a"
"a" "big"
"big" "list"
"list" "of"
"of" "words"
"words" nil
=> nil

Putting the << nil gives the last word something to be spit out of the
each_cons with.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
K

Ken Bloom

It is kind of hard to explain but I have a long string that someone
typed into a form which was then saved into a database. They used made
up tags such as "<choose1>, </choose1>, <row>, </row>, <tbl> ..." I
have to translate the string into html and add javascript etc. based on
the different tags they wrote.

I have it working for everything except one task. In this one task I
want to pass the words between two tags as a value for the javascript.
The problem is it is pulling the phrase apart at every space. I know I
can add .each(???) and have it pull everything apart at the ??? but it
would involve a lot of adding ??? to this long string.

What I am trying to do is add a while loop inside the block that will
look ahead at the next substring to see if it is a "</choose1>". If it
is I want it to exit the while loop and go on without throwing away the
"</choose1>" tag because it is needed next time through the block.

Sorry if that didnt make sense at all.

Might I suggest that using REXML (or another XML parser) may make more
sense for what you're trying to do? First navigate your way through the
tag structure, then you can get the text elements that you're trying to
find.
 
B

ben baka

[Note: parts of this message were removed to make it a legal post.]

I think this might.

string = "This is a big old string"

#Instead of iterating you could match text "big"
find = string.match('big')

#to see if "big" is in the next block of the sentence do this.
puts find.post_match.match('old')

Thanks.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top