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 8813073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:45:11+00:00 2026-06-14T03:45:11+00:00

Possible Duplicate: In Perl, is there a built in way to compare two arrays

  • 0

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

I need to compare arrays with a function that should return:

  • true if all elements are equal when compared pairwise
  • true if all elements are equal or the element in the first array is undefined when compared pairwise
  • false in all other cases

in other words, if the sub is called “comp”:

@a = ('a', 'b', undef, 'c');
@b = ('a', 'b', 'f', 'c');
comp(@a, @b); # should return true
comp(@b, @a); # should return false

@a = ('a', 'b');
@b = ('a', 'b', 'f', 'c');
comp(@a, @b); # should return true

the obvious solution would be to do pairwise compares between the two arrays, but I’d like it to be faster than that, as the comparisons are run multiple times over a large set of arrays, the and the arrays may have many elements.

On the other hand, the contents of the arrays to be compared (i.e.: all the possible @b’s) is pre-determined and does not change. The elements of the arrays do not have a fixed length, and there is no guarantee as to what chars they might contain (tabs, commas, you name it).

Is there a faster way to do this than pairwise comparison? Smart match won’t cut it, as it returns true if all elements are equal (an therefore not if one is undef).

Could packing and doing bitwise comparisons be a strategy? It looks promising when I browse the docs for pack/unpack and vec, but I’m somewhat out of my depth there.

Thanks.

  • 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-06-14T03:45:13+00:00Added an answer on June 14, 2026 at 3:45 am

    Perl can compare lists of 10,000 pairwise elements in about 100ms on my Macbook, so first thing I’ll say is to profile your code to make sure this is actually the problem.

    Doing some benchmarking, there’s a few things you can do to speed things up.

    • Make sure to bail on the first failure to match.

    Assuming you have a lot of comparisons which don’t match, this will save HEAPS of time.

    • Check up front that the arrays are the same length.

    If they arrays aren’t the same length, they can never match. Compare their sizes and return early if they’re different. This avoids needing to check this case over and over again inside the loop.

    • Use an iterator instead of a C-style for loop.

    Iterating pair-wise you’d normally do something like for( my $idx = 0; $idx <= $#a; $idx += 2 ) but iterating over an array is faster than using a C-style for loop. This is an optimization trick of Perl, its more efficient to do the work inside perl in optimized C than to do it in Perl code. This gains you about 20%-30% depending on how you micro-optimize it.

    for my $mark (0..$#{$a}/2) {
        my $idx = $mark * 2;
        next if !defined $a->[$idx] || !defined $b->[$idx];
        return 0 if $a->[$idx] ne $b->[$idx] || $a->[$idx+1] ne $b->[$idx+1];
    }
    return 1;
    
    • Precompute the interesting indexes.

    Since one set of pairs is fixed, you can produce an index of which pairs are defined. This makes the iterator even simpler and faster.

    state $indexes = precompute_indexes($b);
    
    for my $idx ( @$indexes ) {
        next if !defined $a->[$idx];
        return 0 if $a->[$idx] ne $b->[$idx] || $a->[$idx+1] ne $b->[$idx+1];
    }
    
    return 1;
    

    With no nulls this is a performance boost of 40%. You get more beyond that the more nulls are in your fixed set.

    use strict;
    use warnings;
    use v5.10;  # for state
    
    # Compute the indexes of a list of pairs which are interesting for
    # comparison: those with defined keys.
    sub precompute_indexes {
        my $pairs = shift;
    
        die "Unbalanced pairs" if @$pairs % 2 != 0;
    
        my @indexes;
        for( my $idx = 0; $idx <= $#$pairs; $idx += 2 ) {
             push @indexes, $idx if defined $pairs->[$idx];
         }
    
        return \@indexes;
    }
    
    sub cmp_pairs_ignore_null_keys {
        my($a, $b) = @_;
    
        # state is like my but it will only evaluate once ever.
        # It acts like a cache which initializes the first time the
        # program is run.
        state $indexes = precompute_indexes($b);
    
        # If they don't have the same # of elements, they can never match.
        return 0 if @$a != @$b;
    
        for my $idx ( @$indexes ) {
            next if !defined $a->[$idx];
            return 0 if $a->[$idx] ne $b->[$idx] || $a->[$idx+1] ne $b->[$idx+1];
        }
    
        return 1;
    }
    

    I’m still convinced this is better to do in SQL with a self-join, but haven’t worked that out.

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

Sidebar

Related Questions

Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
Possible Duplicate: Python equivalent to perl -pe? Is there a way to process each
Possible Duplicate: Comparing Two Arrays Using Perl How can I print values which exist
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Is there SQL parameter binding for arrays? I was wondering if there
Possible Duplicate: How to join two substrings in perl How could I find a
Possible Duplicate: When should I use the & to call a Perl subroutine? I
Possible Duplicate: What's the easiest way to install a missing Perl module? I'm attempting
Possible Duplicate: In Perl, how can I check from which module a given function
Possible Duplicate: In Perl, how can I check from which module a given function

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.