In Perl, how can I generate all possible combinations of a list?

后端 未结 7 1265
执念已碎
执念已碎 2020-12-16 12:39

I have a file with a list, and a need to make a file that compares each line to the other. for example, my file has this:

AAA  
BBB  
CCC  
DDD  
EEE

I w

7条回答
  •  臣服心动
    2020-12-16 13:07

    How about:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dump qw(dump);
    
    my @in = qw(AAA BBB CCC DDD EEE);
    my @list;
    while(my $first = shift @in) {
        last unless @in;
        my $rest = join',',@in;
        push @list, glob("{$first}{$rest}");
    }
    dump @list;
    

    output:

    (
      "AAABBB",
      "AAACCC",
      "AAADDD",
      "AAAEEE",
      "BBBCCC",
      "BBBDDD",
      "BBBEEE",
      "CCCDDD",
      "CCCEEE",
      "DDDEEE",
    )
    

提交回复
热议问题