http://www.perlmonks.org?node_id=480855


in reply to Very Large Hex Combinations

People have suggested ways of working with numbers. I think it's faster to work with strings in this case (because it reduces greatly the number of calls to sprintf):

my @bytes = map { sprintf('%02X', $_) } 0..255; foreach my $i (@bytes) { foreach my $j (@bytes) { foreach my $k (@bytes) { foreach my $l (@bytes) { foreach my $m (@bytes) { foreach my $n (@bytes) { print("$i$j$k$l$m$n\n"); }}}}}}

Or you could use NestedLoops in Algorithm::Loops:

use Algorithm::Loops qw( NestedLoops ); my @bytes = map { sprintf('%02X', $_) } 0..255; my $num_bytes = 6; NestedLoops( [ ( [ @bytes ] ) x $bytes ], sub { print(join('', @_), "\n"); } );

Replies are listed 'Best First'.
Re^2: Very Large Hex Combinations
by danambroseUK (Beadle) on Aug 04, 2005 at 15:08 UTC
    Thanks all for the *very* quick replies....


    Ikegami's idea with using strings seems to be quicker - my next question is how would I expand the bellow code to produce larger hex patterns?

    my @bytes = map { sprintf('%02X', $_) } 0..255; foreach my $i (@bytes) { foreach my $j (@bytes) { foreach my $k (@bytes) { foreach my $l (@bytes) { foreach my $m (@bytes) { foreach my $n (@bytes) { print("$i$j$k$l$m$n\n"); }}}}}}


    Many thanks
    Dan
      How large? That produces all patterns from 000000000000 to FFFFFFFFFFFF. Add more loops to make it bigger, or increase $num_bytes in my second snippet.
        i think one should use recursion here, something like this maybe
        use strict; use warnings; my @charset = ( 0..9, "A".."F" ); rec( 8 ); sub rec { my $deep = shift; my $string = shift || ''; if ( $deep-- ) { for my $char ( @charset ) { rec( $deep, $string . $char ); } } else { print "$string\n"; } }
        Its a crazy idea but what I am trying to do is create every combination possible of a 640x480 bitmap file.

        Yes this would take forever to do - but just imagine if every photo was created - we would have every photo of everything. Every photo ever taken and every photo to be taken- Effectivly a time machine!!

        Jokes aside, how would this be achieved?


        Thanks,
        Dan