Analyze digits from the argv command line

advertisements

I want to change a perl script that executes a loop some times, and I want to pass the number of loops by command line option. The program now receives some options, then I need to change it to receive a new parameter, but it is the first time I see a perl script, then I don't know how to change. The start of program (to parse command line options) is:

if ($#ARGV >= 1) {
   for ($i = 1; $i <= $#ARGV; $i++) {
       if ($ARGV[$i] =~ /^\-/) {
           if ($ARGV[$i] =~ /\-test/) {
               //do something
           }
       } else {
            //do something other
       }
  }
}

I think that I must put something like:

if ($ARGV[$i] =~ /^\-L40/)

But it only match to 40, I don't know how to parse the number attached to the -L parameter to use for the loop limit.

Thanks in advance and sorry if there is any similar question, but I don't find any.


Something like:

my $loopLimit = 1;  # default

if ($#ARGV >= 1)
{
  for ($i = 1; $i <= $#ARGV; $i++)
  {
    if ($ARGV[$i] =~ /^\-/)
    {
      if ($ARGV[$i] =~ /\-test/)
      {
          # do something
      }
      elsif ($ARGV[$i] =~ /\-L(\d+)/)  # -L followed by digits
      {
          $loopLimit = $1;
      }
    }
    else
    {
        # do something other
    }
  }
}