How can I compare different elements of array in Perl?

后端 未结 2 466
鱼传尺愫
鱼传尺愫 2021-01-07 08:12

I am new to this field. So kindly go easy on me. I have two arrays:

@array1 = (\"ABC321\", \"CDB672\", \"PLE89\",....);

@array2 = (\"PLE89\", \"ABC678\", \"         


        
相关标签:
2条回答
  • 2021-01-07 09:00

    You can use a hash as a lookup device and get an O(m+n) solution (where m is the length of array1 and n is the length of array2).

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @array1 = qw(ABC321 CDB672 PLE89);
    my @array2 = qw(PLE89  ABC678 LMD789);
    
    my %seen;
    
    for my $item (@array1) {
        die "not a valid item: $item"
            unless my ($key) = $item =~ /([A-Z]+)/;
    
        #we are using an array to hold the items in case
        #the same key shows up more than once in an array
        #this code can be simpler if you can guarantee 
        #that the keys are unique
        push @{$seen{$key}}, $item;
    }
    
    for my $item (@array2) {
        die "not a valid item: $item"
            unless my ($key) = $item =~ /([A-Z]+)/;
        if (exists $seen{$key}) {
            print "$item is in array1, it matches @{$seen{$key}}\n";
        } else {
            print "$item is not in array1\n";
        }
    }
    
    0 讨论(0)
  • 2021-01-07 09:00

    Language-agnostic suggestion would be to sort both arrays first (should take you O(n lg(n)), then compare with two iterators in linear time. If performance is not an issue, just keep it simple and go with quadratic number of pair-wise comparisons. While sorting you can also get rid of digits in the end.

    0 讨论(0)
提交回复
热议问题