M
MJS
I can't figure out with whats wrong with this code. Please help.
It doesn't produce the required result.
After the user inputs the value, the script should insert the number
of lines according to that number right after matching a text pattern.
(e.g if input 3, 3 lines should be inserted).
Also, is there anyway to check whether the input is a natural number
and not something else.
================================================
use Tie::File;
use strict;
print "Please enter a positive integer for Data = ";
$data=<STDIN>;
#print "Data = $data";
#line counter
$linecounter = 0;#not used anywhere so far.
# open for update
open(FILE, '+<', 'data.txt') or die "Can't open the file for update:
$!";
# tie @array to filename using Tie::File
tie @array, 'Tie::File', data.txt or die $!;
while(<FILE>){
if ( /\n/) {
$linecounter +=1; #not used yet
if (/some pattern match/ ){
for($n=0; $n <= $data; $n++){
unshift (@array, "sometext"."$n" . "some text "."$n \n") ;
}
}
}
}
untie @array;
close(FILE);
==================
It doesn't produce the required result.
After the user inputs the value, the script should insert the number
of lines according to that number right after matching a text pattern.
(e.g if input 3, 3 lines should be inserted).
Also, is there anyway to check whether the input is a natural number
and not something else.
================================================
use Tie::File;
use strict;
print "Please enter a positive integer for Data = ";
$data=<STDIN>;
#print "Data = $data";
#line counter
$linecounter = 0;#not used anywhere so far.
# open for update
open(FILE, '+<', 'data.txt') or die "Can't open the file for update:
$!";
# tie @array to filename using Tie::File
tie @array, 'Tie::File', data.txt or die $!;
while(<FILE>){
if ( /\n/) {
$linecounter +=1; #not used yet
if (/some pattern match/ ){
for($n=0; $n <= $data; $n++){
unshift (@array, "sometext"."$n" . "some text "."$n \n") ;
}
}
}
}
untie @array;
close(FILE);
==================