On Thu, Aug 02, 2001 at 12:32:16PM +0200, Louis Pouzin wrote:
> Hi,
> 
> Here is a test case:
> 
> #!perl -w
> @ARGV=('-x','tonus','mbx','5','2-4','-x');
> $n=0;
> grep {(/^-x$/ && splice (@ARGV,$n,1) && ($n > $#ARGV || ($ext =  
>splice(@ARGV,$n--,1)))) || ++$n}@ARGV;
> print "n=$n, ext=$ext, argv=@ARGV\n";
> __END__
> 
> The output is:
> 
>       # Use of uninitialized value. (.. line 4)
>       n=3, ext=tonus, argv=mbx 5 2-4
> 
> The variables $ext and @ARGV take the values I want, but I can't figure
> out what is the "uninitialized value". Any hint ?

I suspect the problem is that you are splicing elements out of @ARGV inside
your grep of @ARGV.  Don't do that.

grep iterates over a list, and returns only those elements for which the
expression returned true.  If that's not what you want, you should be using
some other kind of loop.


#!perl

$ext = '';

for ($i = 0; $i <= $#ARGV; ) {
  if ($ARGV[$i] eq '-x') {
    splice @ARGV, $i, 1;
    if ($i <= $#ARGV) {
      $ext = splice @ARGV, $i, 1;
    }
  } else {
    $i++;
  }
}

print "i=$i, ext=$ext, argv=@ARGV\n";

__END__


> NB: the goal is to strip "-x aaa" pairs off @ARGV, including the lone
> "-x" at the end, and have the last "aaa" in $ext.

Have you tried one of the Getopt modules, such as Getopt::Std or
Getopt::Long?

Ronald

Reply via email to