473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2665
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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,...

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.