K
kitty
Hi,
I want to extract the numerical value from a file and use command
line switches to edit that value and then rename the file.
Contents of test-file are :
--------------------------------------------------------------------------------------------
<PROP GUID="471138786526914052" EXPR="<E
Parameter_for_how_often_Jacobian_should_be_calculated="0.001"
The_Rounding_Unit="1e-16"
Absolute_Error_Tolerance="1e-15"
Relative_Error_Tolerance="1e-7" />" />
---------------------------------------------------------------------------------------------
For example: I would like to change 0.001 to any value the user inputs
from the command line by saying :
$ perl comm.pl -f test-file -p <new value>
I have posted the code below. it refuses to work. i would be grateful
if you could point out the errors..
Thanks a lot in advance .
<CODE>
#!usr/bin/perl -w
# file: comm.pl.
use strict;
use Getopt::Long;
sub list_param($);
sub list_the($);
sub list_rel($);
sub list_ab($);
sub usage_message();
#--- set global settings ---
my %G;
$G{file} = '';
$G{param} = '';
$G{the} = '';
$G{rel} = '';
$G{ab} = '';
$G{usage_only} = 0;
#--- get command line arguments ---
GetOptions("f|file=s" => \$G{file},
"h|help" => \$G{usage_only},
"p|param=f" => \$G{param},
"t|the=f" => \$G{the},
"r|rel=f" => \$G{rel},
"a|ab=f" => \$G{ab})
or die ("Invalid command option. $!\n");
#--- Open file for editing ---
if ($G{file})
{
my $fname=$G{file};
unless ($fname) { die ("No file specified.\n"); }
unless (-e $fname) { die ("File does not exist.\n");}
unless (-r $fname) { die ("File is not readable.\n");}
my $line;
open (INFO,"$fname") or die ("Cannot open .. $!\n");
while (<INFO>)
{
$line=<INFO>;
my @new2;
if($G{param}) { list_param($G{param}); }
if($G{the}) { list_the($G{the}); }
if($G{ab}) { list_ab($G{ab}); }
if($G{rel}) { list_rel($G{rel}); }
#--- subroutines ---
sub list_param($)
{
if ($line =~
marameterParameter_for_how_often_Jacobian_should_be_calculated="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{param}/;
print $line;
}
}
sub list_the($)
{
if ($line =~ m:The_Rounding_Unit="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{the}/;
}
}
sub list_ab($)
{
if ($line =~ m:Absolute_Error_Tolerance="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{ab}/;
}
}
sub list_rel($)
{
if($line=~ m:Relative_Error_Tolerance="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{rel}/;
}
}
close INFO;
my $timestamp = strftime("%m/%d/%Y_", localtime((stat($G{file}))[9]));
my $file2=$timestamp.$G{file};
rename ($G{file},$file2) or warn "***\n";
}
#--- if help print usage and exit ---
if ($G{usage_only})
{
usage_message();
}
sub usage_message()
{
print qq{
Usage: Command line option file
Sample: [file...] [options]
Where [options] are:
-f, --file Specify <.vlx> filename (with path)
-p, --param Specify new value for The Parameter for
Jacobian unit
-t, --the Specify new value for The Rounding Unit
-r, --rel Specify new value for The Relative error
-a, --abso Specify new value for The Absolute error
-help brief help message
Examples :
comm.pl -f New5.vlx -p 2.5
comm.pl -f Un.vlx -t 1.3
comm.pl -h "
};
I want to extract the numerical value from a file and use command
line switches to edit that value and then rename the file.
Contents of test-file are :
--------------------------------------------------------------------------------------------
<PROP GUID="471138786526914052" EXPR="<E
Parameter_for_how_often_Jacobian_should_be_calculated="0.001"
The_Rounding_Unit="1e-16"
Absolute_Error_Tolerance="1e-15"
Relative_Error_Tolerance="1e-7" />" />
---------------------------------------------------------------------------------------------
For example: I would like to change 0.001 to any value the user inputs
from the command line by saying :
$ perl comm.pl -f test-file -p <new value>
I have posted the code below. it refuses to work. i would be grateful
if you could point out the errors..
Thanks a lot in advance .
<CODE>
#!usr/bin/perl -w
# file: comm.pl.
use strict;
use Getopt::Long;
sub list_param($);
sub list_the($);
sub list_rel($);
sub list_ab($);
sub usage_message();
#--- set global settings ---
my %G;
$G{file} = '';
$G{param} = '';
$G{the} = '';
$G{rel} = '';
$G{ab} = '';
$G{usage_only} = 0;
#--- get command line arguments ---
GetOptions("f|file=s" => \$G{file},
"h|help" => \$G{usage_only},
"p|param=f" => \$G{param},
"t|the=f" => \$G{the},
"r|rel=f" => \$G{rel},
"a|ab=f" => \$G{ab})
or die ("Invalid command option. $!\n");
#--- Open file for editing ---
if ($G{file})
{
my $fname=$G{file};
unless ($fname) { die ("No file specified.\n"); }
unless (-e $fname) { die ("File does not exist.\n");}
unless (-r $fname) { die ("File is not readable.\n");}
my $line;
open (INFO,"$fname") or die ("Cannot open .. $!\n");
while (<INFO>)
{
$line=<INFO>;
my @new2;
if($G{param}) { list_param($G{param}); }
if($G{the}) { list_the($G{the}); }
if($G{ab}) { list_ab($G{ab}); }
if($G{rel}) { list_rel($G{rel}); }
#--- subroutines ---
sub list_param($)
{
if ($line =~
marameterParameter_for_how_often_Jacobian_should_be_calculated="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{param}/;
print $line;
}
}
sub list_the($)
{
if ($line =~ m:The_Rounding_Unit="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{the}/;
}
}
sub list_ab($)
{
if ($line =~ m:Absolute_Error_Tolerance="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{ab}/;
}
}
sub list_rel($)
{
if($line=~ m:Relative_Error_Tolerance="
{
@new2=split(/\"/,$line);
$line=~s/$new2[1]/$G{rel}/;
}
}
close INFO;
my $timestamp = strftime("%m/%d/%Y_", localtime((stat($G{file}))[9]));
my $file2=$timestamp.$G{file};
rename ($G{file},$file2) or warn "***\n";
}
#--- if help print usage and exit ---
if ($G{usage_only})
{
usage_message();
}
sub usage_message()
{
print qq{
Usage: Command line option file
Sample: [file...] [options]
Where [options] are:
-f, --file Specify <.vlx> filename (with path)
-p, --param Specify new value for The Parameter for
Jacobian unit
-t, --the Specify new value for The Rounding Unit
-r, --rel Specify new value for The Relative error
-a, --abso Specify new value for The Absolute error
-help brief help message
Examples :
comm.pl -f New5.vlx -p 2.5
comm.pl -f Un.vlx -t 1.3
comm.pl -h "
};