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

后端 未结 7 1238
执念已碎
执念已碎 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:25

    Use Algorithm::Combinatorics. The iterator based approach is preferable to generating everything at once.

    #!/usr/bin/env perl
    
    use strict; use warnings;
    use Algorithm::Combinatorics qw(combinations);
    
    my $strings = [qw(AAA BBB CCC DDD EEE)];
    
    my $iter = combinations($strings, 2);
    
    while (my $c = $iter->next) {
        print "@$c\n";
    }
    

    Output:

    AAA BBB
    AAA CCC
    AAA DDD
    AAA EEE
    BBB CCC
    BBB DDD
    BBB EEE
    CCC DDD
    CCC EEE
    DDD EEE
    0 讨论(0)
提交回复
热议问题