Sankeerth_1996 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to to search for an multiple words in a file using regular expressions

Replies are listed 'Best First'.
Re: How to to search for an multiple words in a file using regular expressions
by davido (Cardinal) on Oct 12, 2016 at 06:42 UTC

    Crossposted to stackoverflow.

    You are asking for help writing code, but have not provided any code of your own. Typically how this works is you show us how far you've been able to get on your own, and ask questions that we can answer to help you past where you are stuck.

    It is generally expected that you will have done your own research to begin with. In this case, you can and should read perlintro and perlretut. Having read those you can write most if not all of the homework assignment yourself. And if you do get stuck, you will be able to show us the code you've written and where you're having trouble. I'm certain, at that point, that help will be forthcoming.


      I second this, you have to at least attempt the problem yourself before asking for help. It isn't just about integrity; you will never become a good programmer if you just immediately ask for the answer to even such small tasks as this. Granted, you might not actually be interested in programming and are just trying to pass a required class, but there is still a lot to learn logically from problems such as this. It's good mental exercise!

      One tip from me would be that if you are more familiar with another language (say Java or Python), you can try to solve the problem in that language first so you don't get bogged down with syntax, and then apply the same logic/structure in perl.

Re: How to to search for an multiple words in a file using regular expressions
by Random_Walk (Prior) on Oct 12, 2016 at 07:08 UTC

    A rough sketch to get you started. Not tested

    # Hi all , I need a little help , I am new to perl If for example use strict; # A lot of help use warnings; # more help # in a file named out.txt open my $file, '<', 'out.txt' or die "My homework crashed: $!\n"; # And also print 0 if it doesn't find the word. # Make a note we did not find the word yet... my $found = 0; # which contains the following data Apple Banana potato / Ashok is a b +oy / Apple is good / aLL three sentences in three different lines while (my $line = <$file>) { # read the file line at a time in a loop my @words = split /\s+/, $line; # I need to search the first occurrence of apple and Ashok # Check if the line contains apple and Ashok. # Are these variable, or always the same two literals? # Does order matter? # If we did not find them on this line, use 'next' to check the ne +xt line next unless grep { $_ eq 'apple' } @words; # Oeps, not a regular e +xpression next unless grep { $_ eq 'Ashok' } @words; # # print the 3rd word in that line ie "potato", "a" . print $line[2]; # Why 2? :-) # Note we found the word $found = 1; # exit the loop last; } # if we did not find a word, tell the user print $found unless $found; # you may want to print a newline now ...


    Pereant, qui ante nos nostra dixerunt!
      "I've no doubt a friendly Nigerian Prince will fulfill the OP request in a short while."

      oiskuu at Re: Boyer Moore algorithm.

      «The Crux of the Biscuit is the Apostrophe»