All possible letter combinations?

Hi, im new to ruby. It is smiliar to perl. Can anyone convert this
script to ruby?

#!/usr/bin/perl

my $data = '0123456789abcdefghijklmnopqrstuvwxyz';
   my @list = split //, $data;
        for my $i (@list) {
        for my $j (@list) {
        for my $k (@list) {
        for my $l (@list) {
     print "$i $j $k $l\n";
           }
                }
                    }
                       }

···

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

... convert this script to ruby ...

data='0123456789abcdefghijklmnopqrstuvwxyz'
list=data.split("")
list.each do |i|
list.each do |j|
list.each do |k|
list.each do |l|
puts "#{i} #{j} #{k} #{l} "
end
end
end
end

Thanks!

···

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

Logically equivalent on 1.8.7, but _don't_do_it_ (far too slow)...

s = "0123456789abcdefghijklmnopqrstuvwxyz"
b = ""
(s * 4).split(//).combination(4) {|group| b << group.join << "\n"}

...If just printing you could just use "p group.join", or "puts
group". I find it interesting that it is about 5x slower than the
code quoted above on my machine. I suppose it has to do with extra
method calls inside the block, since it is almost on par if you don't
do anything inside the block :slight_smile: Having b as an array without the
join or "\n" basically halts my machine (10 seconds to recognize the
ctrl-c interrupt).

Todd

···

On Thu, Dec 11, 2008 at 7:22 AM, DanDiebolt.exe <dandiebolt@yahoo.com> wrote:

... convert this script to ruby ...

data='0123456789abcdefghijklmnopqrstuvwxyz'
list=data.split("")
list.each do |i|
  list.each do |j|
    list.each do |k|
      list.each do |l|
        puts "#{i} #{j} #{k} #{l} "
      end
    end
  end
end

Alternatively:

  STRING_LEN = 4 # length of each string to output
  BASE = 36 # number of symbols in data string

  (0..BASE ** STRING_LEN - 1).each do |i|
    puts i.to_s(BASE).rjust(STRING_LEN, '0')
  end

Here this runs ~the same speed as the nested loops solution. This won't
work for BASE > 36 however.

···

On Thu, 11 Dec 2008 21:44:31 -0500, Todd Benson wrote:

data='0123456789abcdefghijklmnopqrstuvwxyz'
list=data.split("")
list.each do |i|
  list.each do |j|
    list.each do |k|
      list.each do |l|
        puts "#{i} #{j} #{k} #{l} "
      end
    end
  end
end

Logically equivalent on 1.8.7, but _don't_do_it_ (far too slow)...

s = "0123456789abcdefghijklmnopqrstuvwxyz"
b = ""
(s * 4).split(//).combination(4) {|group| b << group.join << "\n"}