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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:53:20+00:00 2026-06-13T15:53:20+00:00

Given a phrase like I am searching for a text and one text file

  • 0

Given a phrase like “I am searching for a text” and one text file that contains the list of words.

I have to find the whether each and every combination of the word present in the text file.

For example, I have to search for the occurrence “I”, “I am”, “I am searching”, “I am searching for”, “searching for” etc.

I prefer to write this in perl and I needed a optimal solution that runs faster.

Example text file :

I \n
am searching \n
Text \n
searching for \n 
searching for a \n
for searching       ---> my program should not match this 
etc
  • 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-13T15:53:21+00:00Added an answer on June 13, 2026 at 3:53 pm

    The code below prints all the sub_phrases that you want to match.

    $phrase = 'I am searching for a text';
    $\ = "\n";
    
    @words = ();
    print "Indices:";
    while( $phrase =~ /\b\w+\b/g ) {
        push @words, {word => $&, begin => $-[0], end => $+[0]};
    }
    
    $num_words = $#words + 1;
    print 'there are ', $num_words, ' words';
    
    
    for( $i=0; $i<$num_words; $i++ ) {
        for( $j=$i; $j<$num_words; $j++ ) {
            ($start,$finish) = ($words[$i]->{begin}, $words[$j]->{end});
            $sub_phrase = substr $phrase, $start, $finish-$start;
            print "$i-$j: $sub_phrase";
        }
    }
    

    some explanations:

    1. $\ just to make ‘print’ easier
    2. $phrase – using your sample
    3. @words is an array of references to records
    4. each record is a hash with the word itself, index to the beginning and index to the end of the word
    5. I’ve a regular expression, and I’m iterating. I’m looking for a word boundary, 1 or more word character, and a word boundary.
    6. $+ and $- are special variables for the indices of the match of the last RE
    7. $& is a special variable for the match of the last RE
    8. I then have a nested loop: $i, the outer loop variable is the first word. $j is the last word. That covers all the combinations.
    9. I’m calculating $sub_phrase from the beginning of the first word, to the end of the last word.

    To complete your exercise, you want to save all the sub_phrase’s into an array (instead of ‘print’ do ‘push’ into an @permutations). then iterate through your text file, and for each line, try to match against each permutation.

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

Sidebar

Related Questions

Given that the web application doesn't have su privileges, I'd like to execute a
Given I have a long phrase, say, in line :12 , as this example:
I have a specialized string dictionary of (string, string) (_RulesAndTheirDescriptions) that contains the name
I have a number of functions each of which creates one or more figures.
I have a daemon that reads a configuration file in order to know where
I have an input box that uses jQuery autocomplete. I'd like to add a
Given a set of phrases, i would like to filter the set of all
Not really sure how to phrase this other than by example.. Given... DATABASES =
I have a database of song titles. Each song title has an assigned integer
I'm not exactly sure how to phrase this, but here goes... We have a

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.