Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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?


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 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



        I did. Huh. Wow. Thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://334421]
[choroba]: also, does it make any sense? We run different statements generated from input structures, sometimes prepared statements are involved, sometimes not. We want the behaviour to be consistent.
[erix]: I'm not sure. I suppose you could compare behavior via DBD::Pg with behaviour of the naked SQL
[erix]: I mean, SQL with PREPARE

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2017-09-19 13:02 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (220 votes). Check out past polls.