Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

perl strange hash value

by robertw (Sexton)
on Sep 08, 2012 at 14:42 UTC ( #992487=perlquestion: print w/ replies, xml ) Need Help??
robertw has asked for the wisdom of the Perl Monks concerning the following question:

Dear perlmonks, I have an array in a hash in a hash but when i dump the hash i only get this 54/128? What does this mean and how can I resolve this error? Thank you so much in advance:)

Comment on perl strange hash value
Re: perl strange hash value
by CountZero (Bishop) on Sep 08, 2012 at 14:47 UTC
    What do you mean by "when I dump the hash"? Doing a simple print scalar %myhash does not give you the contents of the hash: it tells you how much key-slots are available (second figure) and how much of these are in use (first figure).

    To get the contents of a hash use keys, values or each. Or have a look at Data::Dumper.

    Update: Thanks Davido for pointing out that I missed scalar in my example.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Underneath I first print my hash, then i print a hash stored in a the hash (aegon) but i cant acces the array like this $totalhistoricalhash{Aegon}{$Aegon1} because that prints nothing, and $totalhistoricalhash{Aegon} prints only 11/16. I dont get how to access one of these variables because I am doing something wrong:S. I dumper both by the with perl dumper(/)

      $VAR1 = { 'ArcelorMittal' => '54/128', 'Aperam' => '28/64', 'Unilever' => '246/512', 'Ahold' => '19/32', 'Akzo' => '33/64', 'PostNL' => '141/256', 'DSM' => '104/256', 'Philips' => '135/256', 'TNT' => '219/512', 'Randstad' => '147/256', 'Royal' => '159/256', 'Wolters' => '238/512', 'SBM' => '212/512', 'Reed' => '153/256', 'ASML' => '46/128', 'Heineken' => '120/256', 'Boskalis' => '67/128', 'Aegon' => '11/16', 'Corio' => '74/128', 'Fugro' => '113/256', 'ING' => '57/128', 'KPN' => '129/256', 'TomTom' => '228/512' }; $VAR1 = { 'Aegon2' => [ '4,109', '4,280', '4,09', '4,28' ], 'Aegon7' => [ '4,100', '3,996', '3,98', '4,11' ], 'Aegon3' => [ '4,045', '4,107', '4,03', '4,13' ], 'Aegon14' => [ '4,175', '4,249', '4,18', '4,28' ], 'Aegon1' => [ '4,300', '4,318', '4,29', '4,38' ], 'Aegon4' => [ '4,087', '4,067', '4,06', '4,12' ], 'Aegon13' => [ '4,209', '4,249', '4,16', '4,28' ], 'Aegon10' => [ '4,111', '4,169', '4,10', '4,20' ], 'Aegon5' => [ '4,061', '4,100', '4,05', '4,12' ], 'Aegon9' => [ '4,127', '4,151', '4,12', '4,18' ], 'Aegon11' => [ '4,180', '4,103', '4,07', '4,19' ], 'Aegon12' => [ '4,286', '4,198', '4,16', '4,30' ], 'Aegon6' => [ '4,009', '4,083', '4,00', '4,12' ], 'Aegon8' => [ '4,147', '4,122', '4,08', '4,16' ] };
Re: perl strange hash value
by Jim (Curate) on Sep 08, 2012 at 15:16 UTC
Re: perl strange hash value
by Rudolf (Monk) on Sep 08, 2012 at 16:14 UTC

    The n/n output you are seeing is how many buckets are empty if you think of the hash like buckets that is.

Re: perl strange hash value
by philiprbrenan (Monk) on Sep 08, 2012 at 16:50 UTC

    I think somewhere you must have assigned the result of scalar %hash instead of \%hash. I have taken the liberty of restructuring your code slightly to illuminate the reference I believe you are trying to make:

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); {my %h = qw(1 2 3 4); say "What happens when you apply scalar to a hash is this: ", scalar + %h; } {my $h1 = {'ArcelorMittal' => '54/128','Aperam' => '28/64','Unilever' + => '246/512','Ahold' => '19/32','Akzo' => '33/64','PostNL' => '141/2 +56','DSM' => '104/256','Philips' => '135/256','TNT' => '219/512','Ran +dstad' => '147/256','Royal' => '159/256','Wolters' => '238/512','SBM' + => '212/512','Reed' => '153/256','ASML' => '46/128','Heineken' => '1 +20/256','Boskalis' => '67/128','Aegon' => '11/16','Corio' => '74/128' +,'Fugro' => '113/256','ING' => '57/128','KPN' => '129/256','TomTom' = +> '228/512' }; my $h2 = {'Aegon2' => ['4,109','4,280','4,09','4,28' ],'Aegon7' => +['4,100','3,996','3,98','4,11' ],'Aegon3' => ['4,045','4,107','4,03' +,'4,13' ],'Aegon14' => [ '4,175', '4,249', '4,18', '4,28' ],'Aegon +1' => ['4,300','4,318','4,29','4,38' ],'Aegon4' => ['4,087','4,067', +'4,06','4,12' ],'Aegon13' => [ '4,209', '4,249', '4,16', '4,28' ], +'Aegon10' => [ '4,111', '4,169', '4,10', '4,20' ],'Aegon5' => ['4,0 +61','4,100','4,05','4,12' ],'Aegon9' => ['4,127','4,151','4,12','4,1 +8' ],'Aegon11' => [ '4,180', '4,103', '4,07', '4,19' ],'Aegon12' = +> [ '4,286', '4,198', '4,16', '4,30' ],'Aegon6' => ['4,009','4,083' +,'4,00','4,12' ],'Aegon8' => ['4,147','4,122','4,08','4,16' ]}; my $a; push @$a, $h2->{"Aegon$_"} for 1..14; $h1->{Aegon} = $a; # Please do not use scalar() here pp($h1); say "Aegon,2,1 = ", $h1->{Aegon}[2][1]; }

    Produces:

    { Aegon => [ ["4,300", "4,318", "4,29", "4,38"], ["4,109", "4,280", "4,09", "4,28"], ["4,045", "4,107", "4,03", "4,13"], ["4,087", "4,067", "4,06", "4,12"], ["4,061", "4,100", "4,05", "4,12"], ["4,009", "4,083", "4,00", "4,12"], ["4,100", "3,996", "3,98", "4,11"], ["4,147", "4,122", "4,08", "4,16"], ["4,127", "4,151", "4,12", "4,18"], ["4,111", "4,169", "4,10", "4,20"], ["4,180", "4,103", "4,07", "4,19"], ["4,286", "4,198", "4,16", "4,30"], ["4,209", "4,249", "4,16", "4,28"], ["4,175", "4,249", "4,18", "4,28"], ], Ahold => "19/32", Akzo => "33/64", Aperam => "28/64", ArcelorMittal => "54/128", ASML => "46/128", Boskalis => "67/128", Corio => "74/128", DSM => "104/256", Fugro => "113/256", Heineken => "120/256", ING => "57/128", KPN => "129/256", Philips => "135/256", PostNL => "141/256", Randstad => "147/256", Reed => "153/256", Royal => "159/256", SBM => "212/512", TNT => "219/512", TomTom => "228/512", Unilever => "246/512", Wolters => "238/512", } What happens when you apply scalar to a hash is this: 2/8 Aegon,2,1 = 4,107
Re: perl strange hash value
by kcott (Abbot) on Sep 08, 2012 at 16:52 UTC

    As you've shown no code, this is just a guess.

    I suspect you've written something like:

    $ perl -Mstrict -Mwarnings -E ' my %x; my %y = (a => 1); $x{z} = %y; say $x{z}; ' 1/8

    when you really wanted something more like:

    $ perl -Mstrict -Mwarnings -E ' my %x; my %y = (a => 1); $x{z} = \%y; say $x{z}; say $x{z}{a}; ' HASH(0x7fba8082b168) 1

    Note the assignment of %y in the first example and \%y in the second example.

    For a better answer, follow the guidelines in How do I post a question effectively?

    -- Ken

Re: perl strange hash value
by davido (Archbishop) on Sep 08, 2012 at 17:09 UTC

    When you look at a hash in scalar context you get a rather useless fraction that describes how the buckets are allocated and being used. When you look at a hash in list context, you get a raw dump of key, value, key, value, which is usually what you want.

    my %hash = qw( a 1 b 2 c 3 d 4 ); print scalar %hash, "\n"; # prints "3/8" print %hash, "\n"; # prints c3a1b2d4 but in # an unpredictable order of key, value, key, value.

    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992487]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-07-14 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls