Insert lines in a file

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);
==================
 
T

Thens

On 21 Sep 2003 23:12:02 -0700
(e-mail address removed) (MJS) wrote:

# 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.

This is a faq and you can search the faq using perldoc -q.

perlfaq says

" Assuming that you don't care about IEEE notations like "NaN"
or "Infinity", you probably just want to use a regular
expression.

if (/\D/) { print "has nondigits\n" }
if (/^\d+$/) { print "is a whole number\n" }
if (/^-?\d+$/) { print "is an integer\n" }
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number" }
if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "a C float" }

"



[ code snipped ]
# $data=<STDIN>;
#
# #print "Data = $data";
#

chomp $data; # remove the trailing new line from the number.

[ snip ]


HTH

Regards,
Thens.
 
J

John W. Krahn

MJS said:
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>;

You need to remove the newline at the end of the input from 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:
$!";

Since you are using Tie::File you don't need to open the file.

# tie @array to filename using Tie::File
tie @array, 'Tie::File', data.txt or die $!;
^^^^^^^^
You need to quote strings.

tie my @array, 'Tie::File', 'data.txt' or die "Cannot open data.txt:
$!";

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") ;
}
}
}
}

for ( 0 .. $#array ) {
if ( $array[ $_ ] =~ /some pattern match/ ) {
splice @array, $_ + 1, 0, "sometext"."$n" . "some text "."$n
\n";
last;
}
}

untie @array;
close(FILE);
==================


John
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (MJS) wrote in
I can't figure out with whats wrong with this code. Please help.
It doesn't produce the required result.
....
# 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 $!;

Either tie the file *or* open it yourself; don't do both.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP29SqmPeouIeTNHoEQIPWwCggZyRUmAIJyJppeLRgRf2p4SpgVsAoPjt
WdxbEprGPHnVjtW/XsTfXxCb
=CD4n
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top