473,443 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading & updating certain variable in file

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
Jan 23 '07 #1
3 2668
KevinADC
4,059 Expert 2GB
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.  
Jan 23 '07 #2
KevinADC
4,059 Expert 2GB
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";
Jan 23 '07 #3
Thanks Kevin.
Your code worked fine.

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";
Jan 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: David Alliet | last post by:
Hello, I'm having a bit of a problem with ASP and Excel. A client has developed his own program, which calculates alot of financial stuff, in excel. I'm doing his websites and he has requested...
5
by: Benjamin de Waal | last post by:
Hey all, I'm trying to figure out how to directly write to a device in Windows. Basically, what I'm wanting to do is create an image of a device (specifically, a CompactFlash card that uses a...
1
by: Sori Schwimmer | last post by:
Hi, I am working on an application which involves interprocess communication. More to the point, processes should be able to notify other processes about certain situations, so the "notifyees"...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
3
by: katz911 | last post by:
Hello, I've encountered a strange problem which I can't seem to explain. I've written a simple C++ class that deals with a certian binary file. In one of the methods there, I wish to update a...
7
by: kimmelsd33 | last post by:
I am using VB6. I want to read a tab delimited file, and assign each column value into a variable. If the variable is "-999.25", I want to make it a "0". I then want to reassemble the values, and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.