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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:42:44+00:00 2026-06-06T21:42:44+00:00

I have two arrays of strings with repeated values that I would like to

  • 0

I have two arrays of strings with repeated values that I would like to compare and get the number of add/del/upd(=same) elements:

my @array1 = ("aaa", "bbb", "ccc", "eee", "eee");
my @array2 = ("aaa", "aaa", "bbb", "ccc", "ccc", "ddd", "fff");

I need something like:

add: 4
del: 2
upd: 3

I tried List::Compare:

my @array1 = ("aaa", "bbb", "ccc", "eee", "eee");
my @array2 = ("aaa", "aaa", "bbb", "ccc", "ccc", "ddd", "fff");

my $lc = List::Compare->new(\@array1, \@array2);        
print Dumper "intersection (upd): ".scalar($lc->get_intersection);
print Dumper "only first   (del): ".scalar($lc->get_unique);
print Dumper "only second  (add): ".scalar($lc->get_complement);

But it cannot work for repeated values:

$VAR1 = 'intersection (upd): 3';
$VAR1 = 'only first   (del): 1';
$VAR1 = 'only second  (add): 2';

How can I solve this problem?

  • 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-06T21:42:45+00:00Added an answer on June 6, 2026 at 9:42 pm

    This will do what you need.

    I trust the mechanism is clear. If you need further explanation please ask again.

    use strict;
    use warnings;
    
    my @array1 = qw( aaa bbb ccc eee eee );
    my @array2 = qw( aaa aaa bbb ccc ccc ddd fff );
    
    my %data;
    
    $data{$_}[0]++ for @array1;
    $data{$_}[1]++ for @array2;
    
    my ($add, $del, $upd) = (0, 0, 0);
    
    for (values %data) {
      my ($a1, $a2) = map $_ // 0, @$_[0,1];
      if ($a1 < $a2) {
        $upd += $a1;
        $add += $a2 - $a1;
      }
      else {
        $upd += $a2;
        $del += $a1 - $a2;
      }
    }
    
    printf "add/del/upd = %d/%d/%d\n", $add, $del, $upd;
    

    output

    add/del/upd = 4/2/3
    
    • 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 that I would like to compare for equality:
I have two Arrays of strings, and I would like to find the set
I have two arrays, @names and @employees , that are filled with strings that
I have two arrays containing different keys and values. However, some keys store strings
I have two strings, and I would like to test if they are anagrams
I have two different arrays, one with strings and another with ints. I want
Supposed that I have two arrays: Dim RoomName() As String = {(RoomA), (RoomB), (RoomC),
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
I have two arrays (or arraylists if it is easier) of strings. I need

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.