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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:22:10+00:00 2026-05-23T00:22:10+00:00

So I have an array my @a = (a,b,c,d,e,f) And another array my @b

  • 0

So I have an array my @a = (a,b,c,d,e,f)
And another array my @b = (c,d,e)

I want to find if there are three consecutive elements in @a that match those in @b.
Then, if there are, I want to get the indices those elements live in.

So in the case above I want to get an array such as (2,3,4).

Another example:
my @a = (1,2,3,4,5)
my @b = (2,3)

Output: (1,2)

  • 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-23T00:22:10+00:00Added an answer on May 23, 2026 at 12:22 am

    Here is a general solution. This uses the ‘all’ function, from List::MoreUtils to reduce the comparison to a true/false result, which simplifies the logic a little.

    I put it into the form of a sub to which you pass a reference to any two arrays. The first array ref passed to the function should be the superset, and the second array ref should refer to the subset array.

    What I like about this solution is that it can apply to any two simple arrays (it isn’t constrained to seeking a two-element subset, for example). I did choose a string comparison of the elements (eq) as opposed to numeric (==). That way it works if you have non-numeric elements. However, it will evaluate ’00’ and ‘0’ as non-equal (because they’re not the same string). If you favor a numeric comparison, just find the ‘eq’ and change it to ‘==’.

    Here’s the code:

    use 5.010_001;
    use strict;
    use warnings;
    
    use List::MoreUtils qw/all/;
    
    my @array_a = qw/1 2 3 4 5/;
    my @array_b = qw/2 3/;
    
    {
        local $, = " ";
        my( @results ) = find_group( \@array_a, \@array_b );
        say "Success at ", @results if @results;
    }
    
    
    sub find_group {
        my( $array_1, $array_2 ) = @_;
        foreach my $array_1_idx ( 0 .. $#{$array_1} ) {
            my $moving_idx = $array_1_idx;
            return $array_1_idx .. ( $moving_idx - 1 ) if 
                all { $_ eq $array_1->[$moving_idx++] } @{$array_2};
        }
        return ();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2d array say, Array A[60][150] and have another array, Array B[60][150].
Basically, I have an array, let's say @badvalues . I have another array, let's
Is there a hash implementation around that doens't remember key values? I have to
I have an array of int pointers int* arr[MAX]; and I want to store
I am scraping data from I want to scrape three search engines. In My
So I want to find the best method of allowing my Logger class to
OK, I am sure that there is a simple way to do this but
Possible Duplicate: How to find the kth largest element in an unsorted array of
I am making a chatbox IM. I have a table where I want to
I have following array pattern. Array ( [0] => Array ( [label] => 2011

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.