Perl Hash of Hashes Storing References To Within - Can't use string ("") as a HASH ref -


alright have function generates hash tree dumper prints out this:

$var1 = {   'shaders' => {     'stock_gui.vert' => '',     'stock_font.vert' => '',     'stock_gui.frag' => '',     'stock_font.frag' => ''   },   'textures' => {},   'fonts' => {     'droidsansmono.ttf' => '',     'small' => {       'droidsansmono.ttf' => ''     }   } }; 

now trying dfs iterate example fonts sub tree:

push (@stack, \%{$myhash->{'fonts'}}); 

then in loop:

my $tmphash = pop(@stack); 

then dumper of $tmphash shows:

$var1 = {   'droidsansmono.ttf' => '',   'small' => {     'droidsansmono.ttf' => ''   } }; 

the problem trying access children of hash reference. can count keys , see children. dumper output looks okay. trying like:

foreach $childkey ( keys $tmphash ){    $subchildrencount = scalar keys(%{$tmphash->{$childkey}}); } 

yields error:

can't use string ("") hash ref while "strict refs" in use 

i think because $tmphash hash reference. need dereference somewhere. i've tried many things , yields further issues. appreciated.

if try:

 %{$tmphash->{'small'}} 

then works fine.

update:

  • if string contains '.' error occurs. hard coding 'small' works. hard coding 'stock_gui.vert' fails unless escape '.'. keys not match if escape dot...

as can see running yourself,

use strict; use warnings;  $tmphash = {   'droidsansmono.ttf' => '',   'small' => {     'droidsansmono.ttf' => ''   } };  $subchildrencount = scalar keys(%{$tmphash->{'small'}}); 

the code gives error not give error. suspect doing

my $subchildrencount = scalar keys(%{$tmphash->{'droidsansmono.ttf'}}); 

your hash format doesn't make sense. mixes field names , actual data keys.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -