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.
poj#!/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
|
|---|