Strings iteration

V

Viorel

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!
 
V

Viorel

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(r){|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

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

aa02bb01
aa02bb02
aa02bb03
..
..
aa10bb09
aa10bb10
Can you help?
 
R

Robert Klemme

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

aa02bb01
aa02bb02
aa02bb03
.
.
aa10bb09
aa10bb10
Can you help?

Where's the difference?

robert
 
V

Viorel

Where's the difference?

        robert

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.
 
R

Robert Klemme

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. :) 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
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top