Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: hash to text

by guha (Priest)
on Aug 14, 2004 at 20:23 UTC ( #383010=note: print w/ replies, xml ) Need Help??


in reply to hash to text

So my question is how to make that output look normal(like what it's supposed to be) ?

What ARE you expecting $h{word2} to contain, [[2,3],3] or [2,3,3] ?

Does this make you any wiser or perhaps you ought to check out the docs?

#!perl -w use strict; my %h = ( word1 =>[2,3], word3 =>[1,2] ); $h{word2} = [ $h{'word1'},3], $h{word4} = [ @{ $h{'word1'} },3], print $h{'word2'}->[0]->[0], "\n"; # Alternative1 print @{ @{ $h{'word2'} }[0] }[0], "\n"; # Alternative1 print @{ $h{'word4'}}[0], "\n"; # Alternative2 print $h{'word4'}->[0], "\n"; # Alternative2 __OUTPUT__ 2 2 2 2


Comment on Re: hash to text
Select or Download Code
Re^2: hash to text
by chiburashka on Aug 14, 2004 at 20:56 UTC
    it should contain [[2,3],3].
Re^2: hash to text
by chiburashka on Aug 14, 2004 at 21:19 UTC
    btw, why when i change the code to :
    my %h = ( word1 =>[2,3], word2 =>[1,3], word3 =>[1,2] ); $h{word1} = [ $h{word2},$h{word3}]; $h{word2} = [ $h{word1},$h{word3}]; $h{word3} = [ $h{word1},$h{word2}]; print @{ @{ $h{'word3'} }[0] }[0], "\n";

    it gives me out the output : ARRAY(0x8064e68)

    And, how can i see this in normal now ?

      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
Node Status?
node history
Node Type: note [id://383010]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-07-10 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (213 votes), past polls