In Perl, is there a built in way to compare two arrays for equality?

后端 未结 13 2317
无人及你
无人及你 2020-11-27 17:08

I have two arrays of strings that I would like to compare for equality:

my @array1 = (\"part1\", \"part2\", \"part3\", \"part4\");
my @array2 = (\"part1\", \         


        
13条回答
  •  渐次进展
    2020-11-27 17:19

    Data::Cmp is another recent option. The cmp_data() function operates similarly to the cmp operator (see perlop for cmp usage).

    Example:

    use 5.10;
    use Data::Cmp qw/cmp_data/;
    
    my @array1 = ("part1", "part2", "part3", "part4");
    my @array2 = ("part1", "PART2", "part3", "part4");
    my @array3 = ("part1", "PART2", "part3", "part4");
    
    # sample usage 
    say "1 & 2 are different" if cmp_data(\@array1, \@array2) ;
    sat "2 & 3 are the same" unless cmp_data(\@array2, \@array3) ;
    

    It's also possible to compare hashes and more complicated nested data structures (within reason). For a single module with no non-core dependencies, Data::Cmp is pretty "smart" ;-) ... errm I mean "useful".

提交回复
热议问题