Hi,

My script has 3 options which recieve integers. If any of those options are having a value which is less than zero i want to make them to 100.

eg: if opt2 = 32 and opt3 = 24 i want to make them to 100.

<code=perl>
our $opt1,$opt2,$opt3;
our @opt = (\$opt1,\$opt2,\$opt3);
GetOptions('opt1=i' => \$opt1,
           'opt2=i' => \$opt2,
           'opt3=i' => \$opt3)
check();
sub check
{
  foreach (@opt)
  {
    # error here : The change does not get reflected on the actual options.
    if ($$_ < 100)
    { 
      $$_ = 100;
    }
  }
}

<icode>

Pls help me...

Edited 3 Years Ago by mike_2000_17: Fixed formatting

#!/usr/bin/perl
#getopt03.pl
use strict;
use warnings;

use Getopt::Long;
our ($opt1, $opt2, $opt3);
our @opt = ( \$opt1, \$opt2, \$opt3 );
GetOptions(
    'opt1=i' => \$opt1,
    'opt2=i' => \$opt2,
    'opt3=i' => \$opt3
);

check();
## I don't see a problem. The actual options do reflect the changes.
print "The actual values of the options are:\n"
        . "opt1 = $opt1\n"
        . "opt2 = $opt2\n"
        . "opt3 = $opt3\n";
        
sub check {
    foreach (@opt) {
        if ( $$_ < 100 ) {
            $$_ = 100;
        }
    }
}

Running the above script on the command line gives the following output:

perl getopt03.pl --opt1=150 --opt2=24 --opt3=32
The actual values of the options are:
opt1 = 150
opt2 = 100
opt3 = 100

Edited 6 Years Ago by d5e5: n/a

Hi,

I've modified the code like this : if the option value is less than 100 add a zero in front it. eg: if $opt1 is 23 it should become 023.

i execute my script like this (without including the opt3) : my_script -opt1 80 -opt2 23

foreach my $val (@opt)
   { 
      if (defined $val and $$val < 100 and $$val != 0)
      { 
         $$val = sprintf("0%d",$$val); 
      }
      print "Check : $$val\n"; 
   }

The code works fine. But it gives some warnings (i have used all the headers that you have used).

Use of uninitialized value in numeric lt (<) at ./fun line 417.
Use of uninitialized value in numeric ne (!=) at ./fun line 417.

Can u clarify this.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi,

I've modified the code like this : if the option value is less than 100 add a zero in front it. eg: if $opt1 is 23 it should become 023.

i execute my script like this (without including the opt3) : my_script -opt1 80 -opt2 23

foreach my $val (@opt)
   { 
      if (defined $val and $$val < 100 and $$val != 0)
      { 
         $$val = sprintf("0%d",$$val); 
      }
      print "Check : $$val\n"; 
   }

The code works fine. But it gives some warnings (i have used all the headers that you have used).

Use of uninitialized value in numeric lt (<) at ./fun line 417.
Use of uninitialized value in numeric ne (!=) at ./fun line 417.

Can u clarify this.

Now you are only giving two options, but are you still defining three options, like this?
#our @opt = ( \$opt1, \$opt2, \$opt3 ); #Defining THREE options

If so, when you loop through the @opt array, you will get a warning because you are trying to compare and concatenate \$opt3 which receives nothing from the command line parameters.

Apparently, $val is always defined in your loop, so your test doesn't stop the warning. It is $$val which is not defined because \$opt3 sometimes has no value. The following runs OK for me:

#!/usr/bin/perl
#getopt04.pl
use strict;
use warnings;

use Getopt::Long;
#our ($opt1, $opt2, $opt3); 
#our @opt = ( \$opt1, \$opt2, \$opt3 ); #Defining THREE options
our ($opt1, $opt2); 
our @opt = ( \$opt1, \$opt2 ); #Defining TWO options
GetOptions(
    'opt1=i' => \$opt1,
    'opt2=i' => \$opt2
);

foreach my $val (@opt){
    if ($$val < 100 and $$val != 0){
    $$val = sprintf("0%d",$$val);
    }
    print "Check : $$val\n";
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.