Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:14:22+00:00 2026-06-01T14:14:22+00:00

I’m trying to construct a permutation program in Perl using the NestedLoops function. Here’s

  • 0

I’m trying to construct a permutation program in Perl using the NestedLoops function. Here’s my code:

use strict;
use warnings;
use Algorithm::Loops qw(NestedLoops);

my @a = 'a'..'o';

my $length = 5;
my $start = 0;
my $depth = 2;

NestedLoops([
  [0..$length],
  ( sub {
    $start = 0 if $start == $depth;
    $start++;
    [$start * $length..$start * $length + $length - 1]
  }) x $depth,
], \&permute,);

sub permute {
  my @ind = @_;
  foreach my $i (@ind) {
    print $a[$i];
  }
  print "\n";
}

So I’ve got an array that holds the letters ‘a’ to ‘o’ (size being 15). I’m treating the array as if it had 3 rows, so my imagination of the array is this:

abcde
fghij
klmno

Then each loop corresponds to each row… and I want to build permutations like:

afk
afl
afm
afn
afo
agk  // fails here... I end up getting agg
...

It works for the first 5 values (the entire run of the lowest for loop), but then the second run fails because the last row’s value of $start gets reset to 0… this is a problem because that breaks everything.

So what I want to know is, how can I keep the value of $start persistent based on the level… So what I’m asking for is essentially having constants. My loops really should look like this:

for my $a (0..5) {        # 0 at this level and never change
  for my $b (5..10) {     # $start should be 5 at this level and never change
    for my $c (10..15) {  # $start should be 10 at this level and never change
      permute($a, $b, $c);
    }
  }
}

Now, because I will have a variable length of for loops, I can’t hard code each start value, so I’m looking for a way to initially create those start values, and then keep them for when the loop gets reset.

I realize this is a confusing question, so please ask questions, and I will help clarify.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:14:23+00:00Added an answer on June 1, 2026 at 2:14 pm

    You are making this harder than it has to be.
    Part of the problem is that the documentation for NestedLoops doesn’t go into much detail about how a subroutine reference in the first argument, will be used.


    For the following examples, assume this is written somewhere above them.

    use strict;
    use warnings;
    use Algorithm::Loops qw'NestedLoops';
    

    Really the simplest way to call NestedLoops to get what you want is like this:

    NestedLoops(
      [
        ['a'..'e'],
        ['f'..'j'],
        ['k'..'o'],
      ],
      \&permute
    );
    
    sub permute {
      print @_, "\n";
    }
    

    If you really want the arguments to NestedLoops to be generated on the fly, I would recommend using part from List::MoreUtils.

    use List::MoreUtils qw'part';
    
    my @a = 'a'..'o';
    
    my $length = 5;
    my $index;
    
    NestedLoops(
      [
        part {
          $index++ / $length
        } @a
      ],
      \&permute
    );
    
    sub permute {
      print @_, "\n";
    }
    

    If for some reason you want to call NestedLoops with indexes into the array, It is still easy with part.

    use List::MoreUtils qw'part';
    
    my @a = 'a'..'o';
    
    my $length = 5;
    
    NestedLoops(
      [
        part {
          $_ / $length
        } 0..@a-1
      ],
      \&permute
    );
    
    sub permute {
      print map { $a[$_] } @_;
      print "\n";
    }
    

    Really the main problem you’re having is that the two subroutine references that you give to NestedLoops are modifying the same variables, and they are both called multiple times.
    The best way to fix this is to rely on the last value given to the subroutine when it is called. ( From looking at the implementation, this seems to be closer to how it was meant to be used. )

    my @a = 'a'..'o';
    
    my $length = 5;
    my $depth = 3;
    
    NestedLoops(
      [
        [0..$length-1],
        (sub{
          return  unless @_;
          my $last = pop;
          my $part = int( $last / $length ) + 1; # current partition
          my $start = $part * $length; # start of this partition
          my $end = $start + $length;
          [$start..$end-1] # list of variables in this partition
        }) x ($depth-1)
      ],
      \&permute
    );
    
    sub permute {
      print map { $a[$_] } @_;
      print "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.