Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: write and read the same file not display the output

by Kenosis (Priest)
on Sep 04, 2012 at 23:47 UTC ( #991705=note: print w/ replies, xml ) Need Help??


in reply to write and read the same file not display the output

Another option is to close the file, and then reopen it for reading:

use strict; use warnings; open my $fo, '+>>', "outfile.txt" or die "Error:$!\n"; print $fo "how r u\n"; print $fo "hey suresh their\n"; print $fo "in bangalore\n"; close $fo; open my $fi, '<', "outfile.txt" or die "Error:$!\n"; while ( my $line = <$fi> ) { print "$line"; my $a = index( $line, "suresh" ); print "$a\n"; } close $fi;

Output:

how r u -1 hey suresh their 4 in bangalore -1 how r u -1 hey suresh their 4 in bangalore -1

I'm assuming your meant index instead of substr.


Comment on Re: write and read the same file not display the output
Select or Download Code
Replies are listed 'Best First'.
Re^2: write and read the same file not display the output
by ulaksh (Acolyte) on Sep 05, 2012 at 00:53 UTC

    thanks for the help

    Now I am trying in different way, please correct if I am wrong

    while ( <$fi> ) { #print "$_"; my $a = $1 if /(^$name$)/; print $a; } close $fi;

    But when I run the file output show nothing, and it shows me some message, I am not getting where is the issue please see the below output

    perl readwritefile.pl Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 1. Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 2. Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 3.

      Hello ulaksh, Im guessing youve commented out your print "$_"; on purpose there. Anyway your getting yelled at because when the regex doesnt make a match, $a is not getting defined at all, so it is still undefined. You then try to use it on the next line print $a; You cant print $a because it has no value. It is undefined at the moment. This is also the reason your not getting anything printed to screen. maybe you can try this instead? :

      print $1 if /^($name)$/;

        Good eye, Rudolf! Did you mean:

        print $1 if /^($name)$/;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991705]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2015-07-31 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls