Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Reading and writing back an updated string

by poj (Abbot)
on Jun 26, 2019 at 20:56 UTC ( [id://11101993]=note: print w/replies, xml ) Need Help??


in reply to Reading and writing back an updated string

Open the input file once and use a single while loop.
Pass the number to the querysub as a parameter and return the result.

#!/usr/bin/perl use strict; open my $fh_in,'<','input.txt' or die "Could not open input $!"; open my $fh_out,'>','output.txt' or die "Could not open output $!"; while (my $var = <$fh_in>){ if ( $var =~ /(.*Entry\s*)(\d*)(.*)/ ){ my $qresult = querysub($2); print $fh_out $1.$2.$qresult.$3."\n"; } } close $fh_in; close $fh_out; sub querysub { my ($num) = @_; return "[Result with $num]"; # test } #input.txt # Entry 1 abc # Entry 2 def # entry 3 hij
poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found