Perl: the Markov chain saw PerlMonks

### Re: Very Large Hex Combinations

by ikegami (Pope)
 on Aug 04, 2005 at 14:57 UTC ( #480855=note: print w/replies, xml ) Need Help??

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

Create A New User
Node Status?
node history
Node Type: note [id://480855]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-11-23 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (335 votes). Check out past polls.

Notices?