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
3 2668
Not exactly sure I understand what all you are trying to do, but using this code: - my $file = 'confVTLN.txt';
-
{
-
local @ARGV = ($file);
-
local $^I = '.bac'; #creates a backup of the original file with a .bac extension
-
while( <> ){
-
if(/^(WARPFREQ = )(\d+.?\d*)/) {
-
my $n = $2;
-
$n += .2;
-
print "$1$n$/";
-
}
-
else {
-
print;
-
}
-
}
-
}
-
print "finished";
on this file: - ESCALE = 1.0
-
-
TRACE = 0
-
-
RAWENERGY = F
-
WARPFREQ = 0.88
-
WARPLCUTOFF = 100
-
WARPUCUTOFF = 4000
produces this output: - ESCALE = 1.0
-
-
TRACE = 0
-
-
RAWENERGY = F
-
WARPFREQ = 1.08
-
WARPLCUTOFF = 100
-
WARPUCUTOFF = 4000
-
-
oops, forgot to check for the max value of 1.2, here it s added to the above code: - my $file = 'confVTLN.txt';
-
{
-
local @ARGV = ($file);
-
local $^I = '.bac'; #creates a backup of the original file with a .bac extension
-
while( <> ){
-
if(/^(WARPFREQ = )(\d+.?\d*)/) {
-
my $n = $2;
-
$n += .2;
-
$n = ($n > 1.2) ? 1.2 : $n;
-
print "$1$n$/";
-
}
-
else {
-
print;
-
}
-
}
-
}
-
print "finished";
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: - my $file = 'confVTLN.txt';
-
{
-
local @ARGV = ($file);
-
local $^I = '.bac'; #creates a backup of the original file with a .bac extension
-
while( <> ){
-
if(/^(WARPFREQ = )(\d+.?\d*)/) {
-
my $n = $2;
-
$n += .2;
-
$n = ($n > 1.2) ? 1.2 : $n;
-
print "$1$n$/";
-
}
-
else {
-
print;
-
}
-
}
-
}
-
print "finished";
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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"...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |