Keep It Simple, Stupid PerlMonks

### Re: dynamic map "quadrant" indexing

by tlm (Prior)
 on Aug 14, 2005 at 15:35 UTC ( #483702=note: print w/replies, xml ) Need Help??

Here's an iterative solution, similar in spirit to the algorithm discussed in HOP, flip, and swap:

```use strict;
use warnings;

my \$base = 4;
my \$lvl = shift || 2;

my @indices = indices( \$lvl );
print "\$_\n" for @indices;

sub indices {
my \$lvl = shift;
die if \$lvl < 0;
my \$first = 'A1';
my @index = map \$first, 1..\$lvl;
my @ret;
my \$n = 1;
{
push @ret, join '', @index;
my \$p = \$n;
my \$j = \$#index;
\$index[ \$j--] = \$first, \$p /= \$base until \$p % \$base;
last if \$j < 0;
\$index[ \$j ] =
chr( ord( \$index[ \$j ] ) + 1 ) . substr( \$index[ \$j ], 1 );
++\$n;
redo;
}
return @ret;
}

But it's much easier to do this with globs:

```my \$glob_string = ( '{' . join( ',', 'A'..'D' ) . '}1' ) x \$level;

my @indices;
while ( < \$glob_string > ) { push @indices, \$_ }

the lowliest monk

Replies are listed 'Best First'.
by ikegami (Pope) on Aug 14, 2005 at 17:07 UTC
Or with Algorithm::Loops:
```use Algorithm::Loops qw( NestedLoops );

my \$level = 3;

my @indices = NestedLoops(
[
(['A'..'D']) x \$level
],
sub { join '', map { "\${_}1" } @_ }
);

foreach my \$val (@indices) {
print(\$val\n")
]
or as an interator:
```use Algorithm::Loops qw( NestedLoops );

my \$level = 3;

my \$iter = NestedLoops(
[
(['A'..'D']) x \$level
]
);

while (my @values = \$iter->()) {
my \$val = join '', map { "\${_}1" } @values;

print("\$val\n");
}
by lidden (Curate) on Aug 14, 2005 at 19:51 UTC
Very nice ++

I did not know about this glob behavior. I had to change it a little to make it work exaktly like the op wanted though.
```my \$level = 2;
my \$l = 'A';

for (2 .. \$level){ \$l++; }
my \$glob_string = ( '{' . join( ',', 'A'.. \$l ) . '}1' ) x \$level;
my @indices = glob \$glob_string;

print "@indices\n";

Create A New User
Node Status?
node history
Node Type: note [id://483702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-06-01 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you really want to know if there is extraterrestrial life?

Results (12 votes). Check out past polls.

Notices?