Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
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 about the Monastery: (6)
As of 2014-12-28 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls