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."
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.