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 ...

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: How to to search for an multiple words in a file using regular expressions by Random_Walk
in thread How to to search for an multiple words in a file using regular expressions by Sankeerth_1996

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.