lucastas has asked for the wisdom of the Perl Monks concerning the following question:
I coded the following myscript.pl …
#!/usr/bin/perl $|=1; my %arguments; for( my $arg = 0 ; $arg <= $#ARGV ; $arg++ ) { print $ARGV[$arg]."\n"; $arguments{ $arg } = $ARGV[$arg]; foreach my $arg ( %arguments ) { print "key = >$arg< value = >$arguments{$arg}<\n"; } }
If I run the script like so…
myscript.pl arg1 arg2 agr3
I get the following output.
arg1
key = >0< value = >arg1<
key = >arg1< value = ><
arg2
key = >0< value = >arg1<
key = >arg1< value = ><
key = >1< value = >arg2<
key = >arg2< value = ><
arg3
key = >0< value = >arg1<
key = >arg1< value = ><
key = >1< value = >arg2<
key = >arg2< value = ><
key = >2< value = >arg3<
key = >arg3< value = ><
If I include use strict, I get the error "Use of uninitialized value in concatenation (.) or string at ./myscript.pl line 13." preceeding the print of the element that has the value as the key and no value.
I went a googling and did a search here at the Monistary but was unable to find an answer to the mystery. Can you help me to understand why the hash has a key/value pair of the real key and value, then a key/value pair with the real value as the key and no value to go with it?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash{X}=array[X] results in two hash keys
by choroba (Cardinal) on Jul 13, 2016 at 22:29 UTC | |
|
Re: hash{X}=array[X] results in two hash keys
by kennethk (Abbot) on Jul 13, 2016 at 23:49 UTC |