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

Replies are listed 'Best First'.
Re: reading file
by moritz (Cardinal) on Aug 04, 2009 at 06:56 UTC
    What have you tried so far, where did you get stuck?

    I suggest reading perlintro and perlretut, these documents should contain enough information and examples to get you started.

      i tried by match the string "saravanan" like while(<FILE>) if($_ =~/saravanan/mg){ count++; } but can't match sa\nravanan and sara\nvanan
        The reason for that is two-fold.

        First the regex sees only one line at a time, so it sees for example ravanan but not the sa\n before it. So you have to change the way you read the file and apply the regex

        The second problem is that even if the regex sees the whole file as a single string, /saravanan/ does not match "sa\nravanan", because it doesn't allow a line break in there. So you have to do something to your regex to allow that. Again I'd like to point you to perlretut for that.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: reading file
by ig (Vicar) on Aug 04, 2009 at 07:37 UTC

    It would be helpful if you updated your post using <code></code> tags. It is not clear what your string contains as it is. You can read about <code> tags and other helpful advice about posting on PerlMonks at Writeup Formatting Tips and Posting on PerlMonks.

    Your string appears to have both newline characters and "\n" escape characters. You could clarify the content by posting the output of the command od -c filename.

    You might find it helpful to break your problem down into two steps: first remove the newline characters; then search for "saravanan".

      "a student name is sa\n ravanan. saravanan is studying 3rd standard. i am go with sara\n vanan"
      this above string is in file. I want to match "saravanan". matching count should be 3 thanks
        I could think of this very simple code, it gives you an idea about creating a list, that list is saved in @array and then for each element in the array there happens a match check that increments the count when true, you have each element checked and the checking goes on till all the array elements have been checked for the pattern /\bsaravanan\b/ ...
        !/usr/local/bin/perl $string="a student name is sa\nravanan. saravanan is studying 3rd stan +dard.i am go with sara\nvanan"; $count = 0; $string =~s/\n//g; #removing \n print "$string\n"; @array = split(" ", $string); #making the string into individual +list elements foreach $element(@array){ if($element=~/\bsaravanan\b/){ $count++; } } print "found: $count matches.\n";
        Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind
    A reply falls below the community's threshold of quality. You may see it by logging in.