Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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}; } }

Replies are listed 'Best First'.
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?
[ambrus]: Corion: what I don't understand is, if you make a one-shot AnyEvent timer with short timeout, does this code guarantee that the Prima timer can't queue two timeout events before invoking any of them?
[ambrus]: Because that could break AnyEvent code, which is why I added a guard.
[ambrus]: (It might be easier to use the callback scalar as a guard, as in $c and &$c(); $c = ();)
[Corion]: ambrus: Hurr - yes, that might happen, as Prima will queue lots of stuff (Well, Windows will)
[Corion]: But I'm really amazed that it just took this little code (plus the idle handler I have in mind and something else dk suggested)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2016-12-09 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.