Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: hash to text

by Velaki (Chaplain)
on Aug 14, 2004 at 22:02 UTC ( [id://383020]=note: print w/replies, xml ) Need Help??


in reply to Re^2: hash to text
in thread hash to text

Let's look at your code with some hand calculations thrown in for fun.

#!/usr/bin/perl my %h = ( word1 =>[2,3], word2 =>[1,3], word3 =>[1,2] ); $h{word1} = [ $h{word2},$h{word3}]; # [[1,3],[1,2]] $h{word2} = [ $h{word1},$h{word3}]; # [[[1,3],[1,2]],[1,2]] $h{word3} = [ $h{word1},$h{word2}]; # [[[1,3],[1,2]],[[[1,3],[1,2]],[1 +,2]]] print @{ @{ $h{'word3'} }[0] }[0], "\n";

If you look at the data above, you'll see that you're dereferencing the structure twice. Well, when looking at the zeroeth element, you'll notice that your print statement above gives a reference to an array, specifically [1,3].

To print it, you will need to deref it one more time.

print @{ @{ $h{'word3'} }[0] }[0]->[0], "\n"; # prints "1" print @{ @{ $h{'word3'} }[0] }[0]->[1], "\n"; # prints "3"

When in doubt, walk through your data by hand; use Data::Dumper; inspect things.

Hope this helps,

-v
"Perl. There is no substitute."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found