Strings iteration

I have some names like aaxxbbyy where xx is ‘01’…‘10’ and yy is also
‘01’…‘10’. I think there is a simple, rubyst way of iterating through
them, and I ask for your help in finding it. Thank you in advance!

Hello,

2010/5/8 Viorel [email protected]:

I have some names like aaxxbbyy where xx is ‘01’…‘10’ and yy is also
‘01’…‘10’. I think there is a simple, rubyst way of iterating through
them, and I ask for your help in finding it. Thank you in advance!

Perhaps this would do what you are looking for ?

a = ‘aa01bb01’
10000.times {puts a.succ!}

Cheers,

On Sat, May 8, 2010 at 12:15 PM, Viorel [email protected] wrote:

I have some names like aaxxbbyy where xx is ‘01’…‘10’ and yy is also
‘01’…‘10’. I think there is a simple, rubyst way of iterating through
them, and I ask for your help in finding it. Thank you in advance!

Without knowing what aa and bb look like I can only guess.
Supposing that aa and bb are not digits I would do the following:

scan( /\d\d?/) # → array of matches

or e.g

scan( /\d\d?/ ){ | a_match| do_whatever_pleases_you_with( a_match ) }

HTH
R.

On May 8, 2010, at 10:30 AM, Robert K. wrote:

=> “01”…“10”
aa10bb10
=> nil
irb(main):007:0>

Kind regards

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I’m guessing the OP expects a series of 100 names:
aa01bb01
aa01bb02

aa01bb10
aa02bb01

aa10bb09
aa10bb10

So iterate both parts separately:

xx_range = ‘01’…‘10’
yy_range = ‘01’…‘10’
xx_range.each do |xx|
yy_range.each do |yy
puts “aa#{xx}bb#{yy}”
end
end

Or wrap that up in a method and replace puts with yield

def names
xx_range = ‘01’…‘10’
yy_range = ‘01’…‘10’
xx_range.each do |xx|
yy_range.each do |yy
yield “aa#{xx}bb#{yy}”
end
end
end

names.each {|name| puts name }

Reasonably simple and certainly rubyist :wink:

-Rob

Rob B.
http://agileconsultingllc.com
[email protected]

[email protected]

On May 8, 5:28 pm, Robert K. [email protected] wrote:

=> “01”…“10”
aa10bb10
=> nil
irb(main):007:0>

Kind regards

    robert


remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

Thank you, Robert! it is a begining, but I need somethink like:
aa01bb01
aa01bb02
aa01bb03
aa01bb04
.
.
.

aa02bb01
aa02bb02
aa02bb03
.
.
aa10bb09
aa10bb10
Can you help?

Thank you, Robert! it is a begining, but I need somethink like:
aa01bb01
aa01bb02

He already gave you the code:

r.zip®{|xx,yy| puts “aa#{xx}bb#{yy}”}

Simply change the {xx} part to keep it more static (or iterate later,
when yy counted up).

On 08.05.2010 12:12, Viorel wrote:

I have some names like aaxxbbyy where xx is ‘01’…‘10’ and yy is also
‘01’…‘10’. I think there is a simple, rubyst way of iterating through
them, and I ask for your help in finding it. Thank you in advance!

I am not sure I understand exactly what you are trying to do. Does this
help?

irb(main):005:0> r = ‘01’ … ‘10’
=> “01”…“10”
irb(main):006:0> r.zip®{|xx,yy| puts “aa#{xx}bb#{yy}”}
aa01bb01
aa02bb02
aa03bb03
aa04bb04
aa05bb05
aa06bb06
aa07bb07
aa08bb08
aa09bb09
aa10bb10
=> nil
irb(main):007:0>

Kind regards

robert

On 05/08/2010 09:20 PM, Viorel wrote:

=> “01”…“10”
aa10bb10
=> nil
irb(main):007:0>

aa02bb02
aa02bb03
.
.
aa10bb09
aa10bb10
Can you help?

Where’s the difference?

robert

On Sun, May 9, 2010 at 9:30 AM, Viorel [email protected] wrote:

I have some names like aaxxbbyy where xx is ‘01’…‘10’ and yy is also
aa03bb03
Thank you, Robert! it is a begining, but I need somethink like:
aa02bb03
.
.
aa10bb09
aa10bb10
Can you help?

The difference is that I have to iterate through yy for a given xx.
xx=‘01’, yy=‘01’…‘10’, so I have 100 outputs, not only 10.

(1…10).each do |x|
(1…10).each do |y|
puts “aa%02dyy%02d” % [ x, y]
end
end


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On May 9, 12:28 pm, Robert K. [email protected] wrote:

‘01’…‘10’. I think there is a simple, rubyst way of iterating through
aa04bb04
aa01bb01
.
remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/
The difference is that I have to iterate through yy for a given xx.
xx=‘01’, yy=‘01’…‘10’, so I have 100 outputs, not only 10.

On Sun, May 9, 2010 at 5:26 PM, Rick DeNatale [email protected]
wrote:

puts “aa%02dyy%02d” % [ x, y]
end
end

I’m reminded of this Ruby-Talk post!

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/95623
Subject: Re: How to pass a given block to a subroutine
From: Simon S.
Date: Tue, 23 Mar 2004 23:44:37 +0900
References: 95616 95617

On Tue, 23 Mar 2004 23:37:14 +0900, ts wrote:

“L” == Lothar S. <mailinglists / scriptolutions.com> writes:
L> def foo (*args)
def foo(*args, &block)
L> subroutine *args
subroutine(*args, &block)
L> end
Guy Decoux

Im too slow… Decoux you are too fast… :wink:


I’d remembered that Simon S. was replying to Guy Decoux, but
I’d forgotten that the original question was from Lothar S., so
it’s nice that three people of whom I have fond ruby-talk memories
were involved in that exchange. (And I wish I’d paid more attention at
the time to what was being said in that thread about passing blocks as
Proc object arguments to methods: if I had, I would have had a much
cleaner version of something I was trying to do about two years ago,
instead of making the cleaner version only this year!)

That said, using the ranges of the original question in this thread,
something like this also seems to work if the ranges for both xx and
yy are the same. (If they aren’t, use a separate range for yy.)

xx = “09” … “10” # using “09” instead of “00” to reduce the output
xx.each do |x|
xx.each do |y|
puts “aa” + x + “bb” + y
end
end
#=>
aa09bb09
aa09bb10
aa10bb09
aa10bb10

Hi,

You could also use this one liner in ruby-1.9.2(r > 27352), if both
range are identical:

(‘01’…‘03’).to_a.repeated_permutation(2) { |x,y| puts “aa#{x}bb#{y}” }
aa01bb01
aa01bb02
aa01bb03
aa02bb01
aa02bb02
aa02bb03
aa03bb01
aa03bb02
aa03bb03

I agree the obvious and flexible solution is still the 2 nested
a.upto(b) tough ( or (a…b).each{} ).

Regards,
B.D.

Thank you, Robert, that is exactly what I need. You are the best!

On 05/09/2010 03:27 PM, Viorel wrote:

‘01’…‘10’. I think there is a simple, rubyst way of iterating through
aa05bb05
aa01bb03
aa10bb10
Can you help?
Where’s the difference?

    robert


remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

The difference is that I have to iterate through yy for a given xx.
xx=‘01’, yy=‘01’…‘10’, so I have 100 outputs, not only 10.

Yes, now I see it, too. Sorry for my stupidity. :slight_smile: You want all
combinations. The obvious solution is two nested iterations

You could do

irb(main):002:0> ‘01’.upto(‘03’){|xx| ‘01’.upto(‘03’) {|yy| puts
“aa#{xx}bb#{yy}”}}
aa01bb01
aa01bb02
aa01bb03
aa02bb01
aa02bb02
aa02bb03
aa03bb01
aa03bb02
aa03bb03
=> “01”
irb(main):003:0>

or
irb(main):001:0> for xx in ‘01’…‘03’
irb(main):002:1> for yy in ‘01’…‘03’
irb(main):003:2> puts “aa#{xx}bb#{yy}”
irb(main):004:2> end
irb(main):005:1> end
aa01bb01
aa01bb02
aa01bb03
aa02bb01
aa02bb02
aa02bb03
aa03bb01
aa03bb02
aa03bb03
=> “01”…“03”
irb(main):006:0>

I picked a small range to not let the number of combinations grow too
large.

Cheers

robert