using command line switches

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="&lt;E
Parameter_for_how_often_Jacobian_should_be_calculated=&quot;0.001&quot;
The_Rounding_Unit=&quot;1e-16&quot;
Absolute_Error_Tolerance=&quot;1e-15&quot;
Relative_Error_Tolerance=&quot;1e-7&quot; /&gt;" />
---------------------------------------------------------------------------------------------
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 =~
m:parameterParameter_for_how_often_Jacobian_should_be_calculated=&quot:)
{
@new2=split(/\&quot;/,$line);
$line=~s/$new2[1]/$G{param}/;
print $line;
}
}

sub list_the($)
{
if ($line =~ m:The_Rounding_Unit=&quot:)
{
@new2=split(/\&quot;/,$line);
$line=~s/$new2[1]/$G{the}/;
}
}

sub list_ab($)
{
if ($line =~ m:Absolute_Error_Tolerance=&quot:)
{
@new2=split(/\&quot;/,$line);
$line=~s/$new2[1]/$G{ab}/;
}
}

sub list_rel($)
{
if($line=~ m:Relative_Error_Tolerance=&quot:)
{
@new2=split(/\&quot;/,$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 "
};
 
P

Paul Lalli

kitty said:
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="&lt;E
Parameter_for_how_often_Jacobian_should_be_calculated=&quot;0.001&quot;
The_Rounding_Unit=&quot;1e-16&quot;
Absolute_Error_Tolerance=&quot;1e-15&quot;
Relative_Error_Tolerance=&quot;1e-7&quot; /&gt;" />
---------------------------------------------------------------------------------------------
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..

It would take a generous person indeed to read through that mountain of
code to find out "the errors" when you gave absolutely NO indication of
how it "refuses to work". What happens? The file isn't modified? The
0.001 is deleted but not replaced? It's replaced with the wrong thing?
Syntax errors? Infinite Loop? Segfault?

Please go read the posting guidelines for this group. Then follow
their advise - create a SHORT BUT COMPLETE program that demonstrates
your problem, providing sample input, desired output, and actual
output. Do not simply copy and paste everything you have. Partition
your problem down to the smallest possible script that still exhibits
the problem. Doing this very often results in you finding the bug
yourself. But if you still can't, post that shortest possible problem
to this group, following the Posting Guidelines, and you'll quite
likely receive the help you desire.

Paul Lalli
 
K

kitty

Hi,
Iam sorry for posting so shabbily.. I was in a hurry..
I guess i have isolated the two trouble points i could not rectify.
1) Does the command line accept more than 2 switches with values ?
2) the regular expression error : to extract the numerical value from
the below line :
" Absolute_Error_Tolerance=&quot;1e-15&quot; "

<code>
open FH,$file or die $!;
while (<FH>)
{
$line=<FH>; or should i say @line=<FH>; ?
sub list_ab($)
{
if ($line =~ m:Absolute_Error_Tolerance=&quot:)
{
$line=$line1;
@new2=split(/\&quot;/,$line1);
$line1=~s/$new2[1]/$G{ab}/;
}
}
</code>

Iam new to perl, please bear with my doubts... thankyou again.
 
J

Josef Moellers

kitty said:
Hi,
Iam sorry for posting so shabbily.. I was in a hurry..
I guess i have isolated the two trouble points i could not rectify.
1) Does the command line accept more than 2 switches with values ?

Just try it and see what happens.
I doubt that you'll let the magic smoke out of the components just be
spoecifying two identical switches.
2) the regular expression error : to extract the numerical value from
the below line :
" Absolute_Error_Tolerance=&quot;1e-15&quot; "

<code>
open FH,$file or die $!;
while (<FH>)
{
$line=<FH>; or should i say @line=<FH>; ?
sub list_ab($)
{
if ($line =~ m:Absolute_Error_Tolerance=&quot:)
{
$line=$line1;

Where does line1 come from? What does it contain? Why do you replace
line with line1?
@new2=split(/\&quot;/,$line1);
$line1=~s/$new2[1]/$G{ab}/;
}
}

Again, what happens when you do that?
If you want to know whether that could be re-written: yes.
I'd write that as
if ($line =~ /Absolute_Error_Tolerance=&quot;([^&]+)&quot;/) {
$line1 = $1;
...
}
 
P

Paul Lalli

kitty said:
Hi,
Iam sorry for posting so shabbily..

Clearly, you're not sorry enough. You did absoultely nothing that I
requested in my previous reply. You have not posted sample input or
desired output or actual output. You have not said what actually
happened. You have not read the posting guidelines, which would have
told you to quote appropriate material when you respond.

If you won't abide by such simple requests, why do you believe I should
help you?
I was in a hurry..

So you decided it would be a time-saver to make us work extra hard to
help you?
I guess i have isolated the two trouble points i could not rectify.
1) Does the command line accept more than 2 switches with values ?

What makes you think it doesn't?
2) the regular expression error

*WHAT* regular expression error? You still haven't told us what's
going wrong!
: to extract the numerical value from
the below line :
" Absolute_Error_Tolerance=&quot;1e-15&quot; "

<code>
open FH,$file or die $!;
while (<FH>)
{
$line=<FH>; or should i say @line=<FH>; ?

Neither. You have a misperception about how to read lines from a file.
the while(<FH>) part already reads the next line from the file, and
puts it in $_. By saying $line=<FH>; within the loop, you are reading
the *next* line. In other words, you're only saving every other line,
and throwing away the one right before it. Change your while loop to:
while (my $line = said:
sub list_ab($)

What do you think this is doing? You're declaring a subroutine within
your while loop. Subroutines are declared *once*, at compile time.
None of the below code is ever actually executed.
{
if ($line =~ m:Absolute_Error_Tolerance=&quot:)
{
$line=$line1;

What is this? Where did $line1 come from? Why are you writing over
$line?
@new2=split(/\&quot;/,$line1);
$line1=~s/$new2[1]/$G{ab}/;

$line1 still doesn't have any value. I don't understand what you think
this is doing. Even if $line1 did contain the line from the file
(assuming you actually meant to say `my $line1 = $line;` rather than
`$line = $line1`), simply doing a search-and-replace on that variable
will not change the original file. You need to re-open the file for
writing and print the modified data back to it.
}
}
</code>

Iam new to perl, please bear with my doubts... thankyou again.

There is nothing wrong with being new to anything. There is a lot
wrong with making it harder than it should be for anyone to help you.
Go back to my previous response, and follow all of the advice and
requests therein before responding again.

Paul Lalli
 
T

Tad McClellan

kitty said:
$line=<FH>; or should i say @line=<FH>; ?


They do different things, so you should say whichever one does
whatever it is that you want to be done.

Do you want to read one line or do you want to read all of the lines?

(I'm pretty sure that you don't want to be doing input here at all!)

sub list_ab($)


You don't need that, I recommend deleting it.

What led you to believe that you needed that line?

What do you think that it will do for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top