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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:25:37+00:00 2026-05-26T19:25:37+00:00

I have 2 arrays @a = qw/ A B C D E /; @b

  • 0

I have 2 arrays

    @a = qw/ A B C D E     /;
    @b = qw/ B B C       A /;    

I need to check if the same element appear in the same position of each array

    e.g. 

    $a[2] = "B";
    $b[3] = "C";

if so count the number of times this happened
it need to disregard any blank elements

    e.g.

    $a[6] =  ;
    $b[6] =  ;

comments are most appreciated I like to understand the script.

I tried intersect eq == cmp etc but I can’t quite get it and not quite sure

Thanks in advance.

Here’s my code so far:

#!/usr/bin/perl -w

my @a = <FILE1>;
my @b = <FILE2>;

$occurs = 0; #Using eq
foreach my $letter (@a) {
    if (my $letter2 (@a) eq $letter) { #Syntax error here
        $count ++;
    }
} #syntax error here
  • 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-26T19:25:37+00:00Added an answer on May 26, 2026 at 7:25 pm

    The definition of blank here needs to be clarified. Is it an empty string? An undefined value? Whitespace?

    I’m assuming any element with no non-whitespace characters as blank in the example below, which shows how it could be done with the each @array construct:

    use strict;
    use warnings;
    
    my @a = ( 'A', 'B', 'C', 'D', 'E', ' ', ' ' );  # Can't use qw/ / 'cos
    my @b = ( ' ', 'B', 'C', ' ', ' ', ' ', 'A' );  # it ignores whitespace
    
    my %count;                                      # Store results in a hash
    
    while ( my ( $index, $value ) = each @a ) { # Loop over index & value together
    
        my $otherValue = $b[$index];            # Get the other value in @b
    
        next unless $value =~ /\S/ and $otherValue =~ /\S/;  # Skip if 'blank'
    
        $count{$value}++ if $value eq $otherValue; # Increment counter for that value
    }
    
    print "$_ : $count{$_}\n" for keys %count;     # B : 1
                                                   # C : 1
    
    # Find out total
    
    use List::Util 'sum';                          # No need to reinvent wheel
    print "Sum : ", sum ( values %count ), "\n";   # Sum : 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an array of arrays. I have been using array_map(null,$a,$b,$c) to
I have an array like this: object[] args and need to insert those args
I have an array @test . What's the best way to check if each
I have 2 arrays of 16 elements (chars) that I need to compare and
I have 3 byte arrays in C# that I need to combine into one.
I have two Ruby arrays, and I need to see if they have any
I have huge 3D arrays of numbers in my .NET application. I need to
I have an array which i need to sort its elements by occurrence then
I have an array of shorts (short[]) that I need to write out to
I have an Array of Objects that need the duplicates removed/filtered. I was going

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.