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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:25:47+00:00 2026-06-04T20:25:47+00:00

I have two arrays, one with search terms and another which is multiple lines

  • 0

I have two arrays, one with search terms and another which is multiple lines fetched from a file. I have a nested foreach statement and am searching for for all combinations, but only the very last match is showing even though I know for a fact that there are many other matches!! I have tried many different versions of the code but here is my last one:

open (MYFILE, 'searchTerms.txt');
open (MYFILE2, 'fileToSearchIn.xml');

@searchTerms = <MYFILE>;
@xml = <MYFILE2>;

close(MYFILE2);
close(MYFILE);
$results = "";

foreach $searchIn (@xml)
{
    foreach $searchFor (@searchTerms)
    {
        #print "searching for $searchFor in: $searchIn\n";
        if ($searchIn =~ m/$searchFor/)
        {
            $temp = "found in $searchIn \n while searching for: $searchFor ";
            $results = $results.$temp."\n";
            $temp = "";
        }
    }
}

print $results;
  • 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-04T20:25:48+00:00Added an answer on June 4, 2026 at 8:25 pm

    You should always use strict and use warnings at the start of your program, and declare all variables at the point of their first use using my. This applies especially when you are asking for help with your code as this measure can quickly reveal many simple mistakes.

    As Raze2dust has said it is important to remember that lines read from a file will have a trailing newline "\n" character. If you were checking for exact matches between a pair of lines then this wouldn’t matter, but since it’s not working for you I assume the strings in searchTerms.txt can appear anywhere in the lines of fileToSearchIn.xml. That means you need to use chomp the strings from searchTerms.txt; lines from the other file can stay as they are.

    Things like this are made a lot easier by using the File::Slurp module. It does all the file handling for you and will chomp any newlines from the input text if you ask.

    I have changed your program to use this module so that you can see how it works.

    use strict;
    use warnings;
    
    use File::Slurp;
    
    my @searchTerms = read_file('searchTerms.txt', chomp => 1);
    my @xml = read_file('fileToSearchIn.xml');
    
    my @results;
    
    foreach my $searchIn (@xml) {
      foreach my $searchFor (@searchTerms) {
        if ($searchIn =~ m/$searchFor/) {
          push @results, qq/Found in "$searchIn"\n while searching for "$searchFor"/;
        }
      }
    }
    
    print "$_\n" for @results;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays. One is $categories which contains all categories retrieved from my
Say I have two arrays where one is a permutation of the other: A
I have two arrays the first one has this structure Parameter Keys array array
I have two arrays. One for input boxes and other for checkbox. inputbox[] checkbox[]
I have two arrays, one is very large (more than million entries) and other
I have two arrays. I'd like to merge them into one, but remove the
I have two arrays of data that I'm trying to amalgamate. One contains actual
I have two different arrays. One array, a, for a list of people. My
I have this two arrays. one is generated using a standard SELECT query, the
I have two String arrays a,b. String a [] = {one,two,three}; String b []

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.