Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

hash{X}=array[X] results in two hash keys

by lucastas (Initiate)
on Jul 13, 2016 at 22:25 UTC ( [id://1167739]=perlquestion: print w/replies, xml ) Need Help??

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
    The assignment is OK. The problem lies in the way how you iterate over the hash:
    foreach my $arg ( %arguments ) {

    You want to only iterate over the keys:

    foreach my $arg ( keys %arguments ) {

    Without keys , a hash behaves like a flattened list of key-value pairs, i.e.

    key1, value1, key2, value2, ...

    With warnings on, you'd have been warned about the uninitialized values for some "keys" (i.e. the real values):

    Use of uninitialized value in concatenation (.) or string at ...

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: hash{X}=array[X] results in two hash keys
by kennethk (Abbot) on Jul 13, 2016 at 23:49 UTC
    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.
    <pedantic>That comes from warnings not strict. Errors from strict are fatal, and the output you got above didn't kill the script</pedantic>

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1167739]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found