in reply to Re: reading file
in thread reading file

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: reading file
by Utilitarian (Vicar) on Aug 04, 2009 at 09:20 UTC
    So you need to read the contents of the file into a scalar variable and match the text with possibly multiple white space or litteral "\n" between any of the characters (hint: \s matches any whitespace including newline).

    That should be a clear enough spec, give it a go and get back if you still have an issue.

      no i applied \s but i wont get the result. I think \s works one for string. it is file, so it reading line by line. so that it not count. my suggestion is we should change way reading the file , Then we can get exact result
        Hi saranperl,

        You applied \s??
        \s is a character class and you need to optionally match it or a literal "\n" between each character it has no effect on the nature of the data you are comparing it to.

        You are however correct to say that you need to change the way you read the data in from the file, or at least the way it is represented when comparing to the regular expression

        (hint 2:Look up the input record separator and see what can be done by redefining it (this is sometimes refereed to as slurping a file)

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

    Learning to write programs is not easy. In the beginning there is too much information, making it difficult to distinguish what is important and difficult to know what to do. To overcome these difficulties, you should follow an introductory course in programming. If you are studying on your own, you can find recommendations in Getting Started with Perl (I suggest you start with Where and how to start learning Perl). Find a tutorial introduction and follow it step by step. Don't rush. Study each example carefully to ensure you understand it before moving on. Be patient and diligent.

    It is particularly difficult to learn how to break a complex problem down into simpler problems that can be solved more easily. I think How to Design Programs gives a gentle introduction. It uses Scheme rather than Perl, but the programming skills it teaches can be applied to other languages, including Perl.

    You have been given very good guidance in other responses to your posts here. I wonder if you have read the documentation referenced in those responses. You should read the documentation carefully. If there is anything you don't understand, you can ask questions here.

    In the mean time, here is a program which matches the strings in your file. You should compare this to the other answers you have been given. It is quite consistent and you should try to understand how to progress from the recommendations in those other answers to a program like this, step by step.

    use strict; use warnings; # Read the entire file contents into $content my $file = 'test.txt'; open(my $fh, '<', $file) or die "$file: $!"; my $content = do { local $/; <$fh> }; close($fh); # Remove newlines and "\\n" sequences $content =~ s/\n|\\n//g; # Match strings my @matches = $content =~ m/saravanan/g; print "@matches\n";