Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: dynamic map "quadrant" indexing

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


in reply to dynamic map "quadrant" indexing

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'.
Re^2: dynamic map "quadrant" indexing
by ikegami (Patriarch) 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"); }
Re^2: dynamic map "quadrant" indexing
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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://483702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found