elavazhagan perl wrote:
Hi,
Hello,
I have a program which reads the input file,updates once and outputs in a
new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the output contains the appended data(L1 TO L1000).
Please can you suggest few methods.
It is not clear from your description or from your code exactly what you
want to do. Do you want 1,000 separate files or one file with the data
replicated 1,000 times or something else?
Anyway, let's have a look at the code:
[ Note that you should indent your code consistently to aide in
readability. ]
#! c:/perl/bin/perl
use strict;
my $file = shift;
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";
#File Handling
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";
# Correct typos, preserving case
while (<OLD>) {
#Condition to match the Field ID and incremanet by 20.
#<Field ID="23120">
if($_ =~ /\<Field(.*)\"(.*)\"\>/)
Why are you capturing to $1 when you are not using it?
{
my $val= $2;
my $newval=$val+20;
$_ =~ s/$val/$newval/;
You shouldn't do that. See below for explanation.
print "OLD:$val \t NEW:$newval\n" ;
}
#Condition to match the line and increament by 1.
# <string ID="Name" Value="24G_L24_UnitsQualifier_2123" />
if ($_ =~ /(L\d{2,3})\_/g)
Why are you using the /g global option if the pattern only occurs once
in the string? The data you supplied does not match the pattern
/(L\d{2,3})_/ so is the data wrong or the pattern wrong or something else?
{
my $line = $1;
if($line =~ /(\d+)/g)
Why are you matching \d again when you already know that \d exists in
the string? Why are you using the /g global option if the pattern only
occurs once in the string?
{
my $n_line = $1;
my $newline = $n_line+1;
$_=~ s/$line/L$newline/;
You *really* shouldn't do that.
print "OLD:$line \t NEW:L$newline\n" ;
}
}
#Condition to increment the final segment in the above string
if($_=~ /\<string(.*)L(\d+)(.*)\_(\d+)\"/)
Why are you capturing to $1, $2 and $3 when you are not using them?
{
my $value=$4;
my $newvalue=$value+20;
$_=~ s/$value/$newvalue/;
You are running a pattern match using a string (in this case $value)
that may occur anywhere in the string, not just the place where you
originally found it, and may contain regular expression meta-characters
that could cause the pattern to fail.
print "OLD:$value \t NEW:$newvalue\n" ;
}
(print NEW $_) or die "can't write to $new: $!";
}
close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";
rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
Your while loop would probably be better written as:
# Correct typos, preserving case
while ( <OLD> ) {
#Condition to match the Field ID and incremanet by 20.
#<Field ID="23120">
if ( s/(<Field.*")(\d+)">/ $1 . ( $2 + 20 ) . '">' /e )
{
print "OLD:$2 \t NEW:", $2 + 20, "\n";
}
#Condition to match the line and increament by 1.
# <string ID="Name" Value="24G_L24_UnitsQualifier_2123" />
if ( s/_L(\d{2,3})_/ '_L' . ( $1 + 1 ) . '_' /e )
{
print "OLD:L$1 \t NEW:L", $1 + 1, "\n";
}
#Condition to increment the final segment in the above string
if ( s/(<string.*L\d+.*_)(\d+)"/ $1 . ( $2 + 20 ) . '"' /e )
{
print "OLD:$2 \t NEW:", $2 + 20, "\n";
}
print NEW $_ or die "can't write to $new: $!";
}
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/