Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

strange hash thing going on

by dpath2o (Acolyte)
on Feb 07, 2008 at 00:23 UTC ( [id://666706]=perlquestion: print w/replies, xml ) Need Help??

dpath2o has asked for the wisdom of the Perl Monks concerning the following question:

Does anybody know why this code is not behaving like it should
my @types0 = qw( i i m i m m m i m i ); my @types1 = qw( i i i i m m m i m i ); my @types2 = qw( i i i i m m i i m i ); my @types3 = qw( i i i i m i i i m i ); my @types4 = qw( i i i i m i m i m i ); my @types5 = qw( i i i i m m m i m i ); my @types6 = qw( i i i i i m m i m i ); my @types7 = qw( i i i i m m m i m i ); my @types8 = qw( i i i i m i m i m i ); my @types9 = qw( i i i i m m m i m i ); my @types10 = qw( i i i m m m m i m i ); my @types11 = qw( i i m m m m m i m i ); my @types12 = qw( i i m m m m m i i i ); my @types13 = qw( i i m m m m m i m i ); my @types14 = qw( i i m m m m m i i i ); my %changes = ( "2006_01_01_00_00_00" => @types0, "2006_02_03_19_00_00" => @types1, "2006_03_03_09_00_00" => @types2, "2006_04_14_22_00_00" => @types3, "2006_07_07_22_00_00" => @types4, "2006_07_20_19_00_00" => @types5, "2006_08_10_20_00_00" => @types6, "2006_10_17_21_00_00" => @types7, "2006_11_06_23_00_00" => @types8, "2006_11_27_20_00_00" => @types9, "2006_12_18_21_00_00" => @types10, "2007_03_19_20_00_00" => @types11, "2007_05_09_20_00_00" => @types12, "2007_08_06_16_00_00" => @types13, "2007_09_09_08_00_00" => @types14, ); foreach my $l1 (sort keys %changes) { print "$l1\n"; }

This is what I get when I run this above code:
2006_01_01_00_00_00
2006_03_03_09_00_00
2006_07_07_22_00_00
2006_08_10_20_00_00
2006_11_06_23_00_00
2006_12_18_21_00_00
2007_05_09_20_00_00
2007_09_09_08_00_00
i
m


Obviously somethings wrong ... Any help oh great monastery of perl wisdom?

Replies are listed 'Best First'.
Re: strange hash thing going on
by friedo (Prior) on Feb 07, 2008 at 00:34 UTC

    Your @types arrays are getting flattened to lists in the declaration of your %changes hash, causing every other element in them to become a key. So your hash declaration really evaluates to my %changes = ( "2006_01_01_00_00_00" =>  'i', 'i', 'm', ... and so on.

    (Since every item in your @types arrays is either 'i' or 'm', those keys keep clobbering themselves.) What you want is a hash of arrays, which you can make easily enough by taking a reference to the arrays:

    my %changes = ( "2006_01_01_00_00_00" => \@types0, "2006_02_03_19_00_00" => \@types1, "2006_03_03_09_00_00" => \@types2, "2006_04_14_22_00_00" => \@types3, "2006_07_07_22_00_00" => \@types4, "2006_07_20_19_00_00" => \@types5, "2006_08_10_20_00_00" => \@types6, "2006_10_17_21_00_00" => \@types7, "2006_11_06_23_00_00" => \@types8, "2006_11_27_20_00_00" => \@types9, "2006_12_18_21_00_00" => \@types10, "2007_03_19_20_00_00" => \@types11, "2007_05_09_20_00_00" => \@types12, "2007_08_06_16_00_00" => \@types13, "2007_09_09_08_00_00" => \@types14, );
      Friedo, You're a beautiful golden God of love ... Thank You!
Re: strange hash thing going on
by Cody Pendant (Prior) on Feb 07, 2008 at 03:36 UTC
    Just because someone's got to say it -- when you find yourself doing "Thing_n = 'foo', Thing_n+1 = 'bar', Thing_n+2 = 'baz'" you should hear a voice whispering in your ear "use an array!".


    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found