Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading & updating certain variable in file

Newbie
 
Join Date: Jan 2007
Posts: 2
#1: Jan 23 '07
Hi all,

I am newbie of perl. The story is, i need to read and update one variable from text file (WARPFREQ = 0.88). The value of WARPFREQ will be inceremented by 0.2 up to 1.2. Everytime the value changed, it will be saved to the same line and file. The problem is the line of (WARPFREQ = 0.88) was deleted and the updated line from the process (WARPFREQ = 1.28) was inserted without shifting the below line. Additionally, the incremental process didnot work too. It was supposed to be 1.08 but it was considered as string not a scalar.

I would appreciate any help.

Regards,

indra

filemod.pl

#!/usr/bin/perl
$file = "confVTLN";
$tmp = "confVTLN.tmp";

my $file = shift;
my $tmp = $file . ".tmp";

my $disclaimer = "RAWENERGY";

open(OLD, "< confVTLN") or die "open $file: $!";
open(NEW, "> confVTLN.tmp") or die "open $tmp: $!";

while (my $line = <OLD>) {
print NEW $line or die "print $tmp: $!";

if ($line =~ m/$disclaimer/) {
$line = <OLD>;
($param,$val)=split(/ *= */,$line);
while ($val<=1.2){
$val+=0.2;
$line = join('=', $param,$val);
}
print NEW $line;
}

}
close(OLD) or die "close $file: $!";
close(NEW) or die "close $tmp: $!";

unlink("confVTLN") or die "unlink $file: $!";
rename("confVTLN.tmp", "confVTLN") or die "can't rename $tmp to $file: $!";

1;


confVTLN file:

ESCALE = 1.0

TRACE = 0

RAWENERGY = F
WARPFREQ = 0.88
WARPLCUTOFF = 100
WARPUCUTOFF = 4000

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jan 23 '07

re: Reading & updating certain variable in file


Not exactly sure I understand what all you are trying to do, but using this code:

Expand|Select|Wrap|Line Numbers
  1. my $file = 'confVTLN.txt';
  2. {
  3.    local @ARGV = ($file); 
  4.    local $^I = '.bac'; #creates a backup of the original file with a .bac extension
  5.    while( <> ){ 
  6.       if(/^(WARPFREQ = )(\d+.?\d*)/) {
  7.          my $n = $2;
  8.          $n += .2;  
  9.          print "$1$n$/"; 
  10.       } 
  11.       else { 
  12.          print; 
  13.       } 
  14.    }
  15. }
  16. print "finished";
on this file:

Expand|Select|Wrap|Line Numbers
  1. ESCALE = 1.0
  2.  
  3. TRACE = 0
  4.  
  5. RAWENERGY = F
  6. WARPFREQ = 0.88
  7. WARPLCUTOFF = 100
  8. WARPUCUTOFF = 4000
produces this output:

Expand|Select|Wrap|Line Numbers
  1. ESCALE = 1.0
  2.  
  3. TRACE = 0
  4.  
  5. RAWENERGY = F
  6. WARPFREQ = 1.08
  7. WARPLCUTOFF = 100
  8. WARPUCUTOFF = 4000
  9.  
  10.  
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Jan 23 '07

re: Reading & updating certain variable in file


oops, forgot to check for the max value of 1.2, here it s added to the above code:


Expand|Select|Wrap|Line Numbers
  1. my $file = 'confVTLN.txt';
  2. {
  3.    local @ARGV = ($file); 
  4.    local $^I = '.bac'; #creates a backup of the original file with a .bac extension
  5.    while( <> ){ 
  6.       if(/^(WARPFREQ = )(\d+.?\d*)/) {
  7.          my $n = $2;
  8.          $n += .2;  
  9.          $n = ($n > 1.2)  ? 1.2 : $n;
  10.          print "$1$n$/"; 
  11.       } 
  12.       else { 
  13.          print; 
  14.       } 
  15.    }
  16. }
  17. print "finished";
Newbie
 
Join Date: Jan 2007
Posts: 2
#4: Jan 24 '07

re: Reading & updating certain variable in file


Thanks Kevin.
Your code worked fine.

Quote:

Originally Posted by KevinADC

oops, forgot to check for the max value of 1.2, here it s added to the above code:


Expand|Select|Wrap|Line Numbers
  1. my $file = 'confVTLN.txt';
  2. {
  3.    local @ARGV = ($file); 
  4.    local $^I = '.bac'; #creates a backup of the original file with a .bac extension
  5.    while( <> ){ 
  6.       if(/^(WARPFREQ = )(\d+.?\d*)/) {
  7.          my $n = $2;
  8.          $n += .2;  
  9.          $n = ($n > 1.2)  ? 1.2 : $n;
  10.          print "$1$n$/"; 
  11.       } 
  12.       else { 
  13.          print; 
  14.       } 
  15.    }
  16. }
  17. print "finished";

Reply