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?


In reply to hash{X}=array[X] results in two hash keys by lucastas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.