Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: hash reference syntax?

by Wonko the sane (Deacon)
on Mar 06, 2004 at 00:15 UTC ( #334421=note: print w/ replies, xml ) Need Help??


in reply to hash reference syntax?

Hello,

The reason you are getting that error is just like the error says.
you are trying to use the string 'value' as a hash reference.

print "'hash{key}' is a hash reference\n" if ref(\%{$hash{key}}) eq 'H +ASH';
$hash{key} = 'value'

and you are trying to reference it as a hash by surrounding it with '\%{ }
In Essense you are saying '\%{"value"}'

I'm not really sure what you are doing with this, but you could use some sort of
recursive subroutine to dig down into the hash to get what you want.

Something like this maybe.

#!/usr/local/bin/perl5.6.0 use strict; use warnings; my $hash = { h1 => { ss1 => 'Howdy' }, h2 => { hh1 => { sss1 => 'Hello' } }, s1 => 'Doody', a1 => [], }; dig( $hash ); sub dig { my ( $hole ) = @_; if( ref($hole) eq 'HASH' ) { foreach my $l ( keys %{$_[0]} ) { dig( $_[0]->{$l} ); } } elsif ( ref($hole) eq 'ARRAY' ) { print q{What do I do with an Array ref?\n}; } else { my $leaf = $hole; # leaf in a hole?!? print qq{Found a leaf in an hole! [$leaf]\n}; } }


Comment on Re: hash reference syntax?
Select or Download Code
Re: Re: hash reference syntax?
by Anonymous Monk on Mar 06, 2004 at 00:50 UTC
    Thanks for both of your answers. My error was that I was testing only against 'HASH' while I should have given myself an out. Taking your answers a step further, when I apply all types mentioned for ref() at Perldoc.com as in the following:
    #!/usr/bin/perl use strict; sub traverse($); #my $hash = { # key0 => 'value0', # key1 => { key2 => 'value2' }, # key3 => { key4 => 'value4', key5 => { key6 => 'value6' } } #}; my %hash; $hash{key0} = 'value0'; $hash{key1}{key2} = 'value2'; $hash{key3}{key4} = 'value4'; $hash{key3}{key5}{key6} = 'value6'; traverse(\%hash); sub traverse($) { my $r = shift; if (ref($r) eq 'HASH') { for my $v (values %$r) { traverse($v); } } elsif (ref($r) eq 'SCALAR') { print "SCALAR:\t'$r'\n"; } elsif (ref($r) eq 'ARRAY') { print "ARRAY:\t'$r'\n"; } elsif (ref($r) eq 'REF') { print "REF:\t'$r'\n"; } elsif (ref($r) eq 'LVALUE') { print "LVALUE:\t'$r'\n"; } elsif (ref($r) eq 'CODE') { print "CODE:\t'$r'\n"; } elsif (ref($r) eq 'GLOB') { print "GLOB:\t'$r'\n"; } else { print "unexpected value:\t'$r'\n" } }

    I get the following unexpected (at least to me...) results:

    unexpected value: 'value2' unexpected value: 'value0' unexpected value: 'value6' unexpected value: 'value4'
    How is this possible if I have accounted for all possible types? Thanks.

      How is this possible if I have accounted for all possible types?

      'some string' is a literal. It is not a reference to anything. I presume you though that ref() would report scalar.

      $foo = 'bar'; print '$foo is a ', ref($foo), $/; print '\$foo is a ', ref(\$foo), $/; __DATA__ $foo is a \$foo is a SCALAR

      cheers

      tachyon

        I did. Huh. Wow. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://334421]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-27 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls