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

后端 未结 13 2310
无人及你
无人及你 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:18

    If order and duplicate values do not matter but only values equality (i.e. set comparison), you could use Set::Scalar.

    It overloads common operators such as == or !=.

    my @array1 = ("part1", "part2", "part3", "part4");
    my @array2 = ("part1", "PART2", "part3", "part4");
    
    if ( Set::Scalar->new(@array1) == Set::Scalar->new(@array2) ) {...}
    

    Alternatively, there's also Algorithm::Diff and List::Compare.

提交回复
热议问题