-3

Hi i am working on a script in perl i have seen codes for permutation but my problem is i have an array with values say @array =(john, cena , barrack, obama, donald, trump) value at index 0 is first name and at 1 is last name at 2 is first name again and at 3 is last name and so on. so it should be like

john cena
barrack obama
donald trump

i need permutation for this combination

barrack obama
donald trump
john cena

donald trump
john cena
barrack obama

like this how this can be done!! I have done this with hard coding but i need to make it more generic !

            my $i=0;
            my @array;
            print $fh_testcases "\n${indentation}${tab}$tab'$intent_json_result': [[\n";
            while ((my $key, my $value) = each %{@{$p_phrase_recogslots}[${speech_phrase_counter}-1]}) {
                $array[$i]= $key;
                $array[$i+1]= $value;
                $i= $i+2;
            }
            if ($i == 6){
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"origin\", \"onboard\")\n"; #TODO handle offboard cases
                print $fh_testcases "${indentation}${tab}$tab]]\n";
                print $fh_testcases "${indentation}${tab}},\n";

                print $fh_testcases "${indentation}${tab}\{'$intent_json_result': [[\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";                    
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n";

                print $fh_testcases "${indentation}${tab}\{'$intent_json_result': [[\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n";                    
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";

                print $fh_testcases "${indentation}${tab}\{'$intent_json_result': [[\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n"; 

                print $fh_testcases "${indentation}${tab}\{'$intent_json_result': [[\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";

                print $fh_testcases "${indentation}${tab}\{'$intent_json_result': [[\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[0]\", \"$array[1]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[4]\", \"$array[5]\"),\n";
                print $fh_testcases "${indentation}${tab}$tab$tab(\"$array[2]\", \"$array[3]\"),\n";
            }
simbabque
  • 53,749
  • 8
  • 73
  • 136
H.A
  • 21
  • 3
  • "Permutations" would be printing every possible order those six words can be arranged in, such as "obama john donald cena barrack trump". Is that what you're actually looking for? Because I get the impression it isn't. – Dave Sherohman Jan 18 '18 at 12:18
  • 1
    Just put `"john cena"`, `"barack obama"`, and `"donald trump"` into one string each and then apply the permutation algorithm you already have to that three-element array. You have an algorithm, haven't you? – PerlDuck Jan 18 '18 at 12:26
  • 2
    Please [edit] your question and include a working program. Right now you are missing the variable definition for your data, so we cannot run your code. See [mcve] for more details. – simbabque Jan 18 '18 at 12:39
  • Off-topic, but maybe helpful in general, when you write Perl code, if you are escaping characters in string literals, you are probably doing it wrong. You can make any delimiter character an interpolating quote with the `qq` operator. Your print statements can also be made less messy if you remember that print takes a list of arguments and prints them. So your messy print statements can be reduced to `print $fh $indentation, $tab x $indent_level, qq("$word_list[1]");` For more info see https://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators – daotoad Jan 18 '18 at 23:19

3 Answers3

2
use Algorithm::Loops qw( NextPermuteNum );

my @array = ( "john cena", "barrack obama", "donald trump" );

my @i = 0..$#array;
do {
   say map { "$_\n" } @array[@i];
} while NextPermuteNum(@i);
ikegami
  • 367,544
  • 15
  • 269
  • 518
1
use Algorithm::Permute;

my @array = ( "john cena", "barrack obama", "donald trump" );

my $iterator = Algorithm::Permute->new ( \@array );
while (my @perm = $iterator->next) {
    say map { "$_\n" } @perm;
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
San
  • 226
  • 5
  • 14
-1
  • Get the value of $#array, add 1, divide by 2, and store that number in a variable (call it $v)
  • Get a permutation of the set of numbers [0 .. $v-1]
  • For each member $i of the resulting permutation, print $array[2*$i] $array[2*$i+1]
hymie
  • 1,982
  • 1
  • 13
  • 18