Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 409905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:50:10+00:00 2026-05-12T17:50:10+00:00

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

  • 0

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

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

Is there a built-in way to compare arrays like there is for scalars?
I tried:

if (@array1 == @array2) {...}

but it just evaluated each array in scalar context, and so compared the length of each array.

I can roll my own function to do it, but it seems like such a low-level operation that there should be a built-in way to do it. Is there?

Edit: sadly, I don’t have access to 5.10+ or optional components.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:50:10+00:00Added an answer on May 12, 2026 at 5:50 pm

    There is the new smart match operator:

    #!/usr/bin/perl
    
    use 5.010;
    use strict;
    use warnings;
    
    my @x = (1, 2, 3);
    my @y = qw(1 2 3);
    
    say "[@x] and [@y] match" if @x ~~ @y;
    

    Regarding Array::Compare:

    Internally the comparator compares the two arrays by using join to turn both arrays into strings and comparing the strings using eq.

    I guess that is a valid method, but so long as we are using string comparisons, I would much rather use something like:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use List::AllUtils qw( each_arrayref );
    
    my @x = qw(1 2 3);
    my @y = (1, 2, 3);
    
    print "[@x] and [@y] match\n" if elementwise_eq( \(@x, @y) );
    
    sub elementwise_eq {
        my ($xref, $yref) = @_;
        return unless  @$xref == @$yref;
    
        my $it = each_arrayref($xref, $yref);
        while ( my ($x, $y) = $it->() ) {
            return unless $x eq $y;
        }
        return 1;
    }
    

    If the arrays you are comparing are large, joining them is going to do a lot of work and consume a lot of memory than just comparing each element one by one.

    Update: Of course, one should test such statements. Simple benchmarks:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Array::Compare;
    use Benchmark qw( cmpthese );
    use List::AllUtils qw( each_arrayref );
    
    my @x = 1 .. 1_000;
    my @y = map { "$_" } 1 .. 1_000;
    
    my $comp = Array::Compare->new;
    
    cmpthese -5, {
        iterator => sub { my $r = elementwise_eq(\(@x, @y)) },
        array_comp => sub { my $r = $comp->compare(\(@x, @y)) },
    };
    

    This is the worst case scenario where elementwise_eq has to go through each and every element in both arrays 1_000 times and it shows:

                 Rate   iterator array_comp
    iterator    246/s         --       -75%
    array_comp 1002/s       308%         --
    

    On the other hand, the best case scenario is:

    my @x = map { rand } 1 .. 1_000;
    my @y = map { rand } 1 .. 1_000;
    
                  Rate array_comp   iterator
    array_comp   919/s         --       -98%
    iterator   52600/s      5622%         --
    

    iterator performance drops quite quickly, however:

    my @x = 1 .. 20, map { rand } 1 .. 1_000;
    my @y = 1 .. 20, map { rand } 1 .. 1_000;
    
                  Rate   iterator array_comp
    iterator   10014/s         --       -23%
    array_comp 13071/s        31%         --
    

    I did not look at memory utilization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of strings with repeated values that I would like to
I have two arrays, @names and @employees , that are filled with strings that
I have two string arrays Array1[size] and Array2[size]. They both have the same size.
I have two arrays that both look like this: Array ( [0] => Array
I'm trying to compare two strings and as output I would like a count
I have an array of strings that I am looping through. I would like
Supposed that I have two arrays: Dim RoomName() As String = {(RoomA), (RoomB), (RoomC),
I have two different arrays, one with strings and another with ints. I want
I have two arrays. I'd like to copy ranges of data from one of
I have two cell arrays of strings, and I want to check if they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.