Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

hash to text

by chiburashka (Initiate)
on Aug 14, 2004 at 19:15 UTC ( [id://383002]=perlquestion: print w/replies, xml ) Need Help??

chiburashka has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
Please help a fool, that got a little bit dizzy from hashing.
My code is:
%h = ( 'word1'=>[2,3], 'word2'=>[\$h{'word1'},3], 'word3'=>[1,2] ); print "$h{'word2'}[0]\n";

Now, the output is : SCALAR(0x8064e88)

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

Replies are listed 'Best First'.
Re: hash to text
by antirice (Priest) on Aug 14, 2004 at 20:01 UTC

    Four things:

    1. The way to get the value you want is with ${$h{'word1'}[0]}.
    2. If you'd have employed use strict, you'd notice that %h isn't yet defined when you try to grab $h{'word1'}. The value that $h{'word2}[0] currently contains is a reference to undef.
    3. You could have confirmed all of this by using Data::Dumper with print Dumper(\%h);.
    4. Why are you assigning a reference to an arrayref?

    Proper way to do this would be to do:

    my %h = ( 'word1'=>[2,3], 'word2'=>[undef,3], 'word3'=>[1,2] ); $h{word2}[0] = $h{word1}; print "@{$h{word2}[0]}\n"; __END__

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: hash to text
by been42 (Curate) on Aug 14, 2004 at 20:14 UTC
    Here is the link I keep for when I have reference troubles. I have trouble deciphering parts of it, but what it does do is remind me to always use Data::Dumper when I can't figure something out.

    A few years ago, I would probably have sat for hours putting slashes and curly braces on and around everything without understanding what I was doing. (Who am I kidding? That's exactly what I did.) Now I let Data::Dumper do the heavy lifting for me.

Re: hash to text
by guha (Priest) on Aug 14, 2004 at 20:23 UTC
    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
      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."
      it should contain [[2,3],3].
Re: hash to text
by johnnywang (Priest) on Aug 14, 2004 at 22:47 UTC
    I'm actually a little unsure of what you're trying to do. The other posts are all helpful for fixing the bugs in what you presented. May be you can tell us what you're trying to do? it sounds quite interesting.
Re: hash to text
by perldeveloper (Scribe) on Aug 15, 2004 at 14:16 UTC
    Since your code requires to reference previous hash values from new hash entries, you should populate the hash one entry at a time, instead of sending in the entire list. This gives you the most freedom to intertwine the references, spot recursions and order the assignments according to your underlying logic. That is:
    my %h = (); $h{'word1'} = [2, 3]; $h{'word2'} = [\${'word1'}, 3]; $h{'word3'} = [1, 2];
    Given the way you wrote the assignments, you suggested that you expect every hash entry to be assigned one at a time, which is exactly what the above piece of code does. The reasons why Perl does not assign them the way you'd expect them to is first because it treats the hash entries as a regular list containing a key, a value, another key, another value and so on, and secondly because it calculates the values of every element in this list (key or value) before assigning it to a hash variable. The reason why you got a scalar reference is because Perl treats \undef as a reference to a scalar, a behavior which you cannot avoid even setting warnings and strict on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://383002]
Approved by Corion
Front-paged by been42
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found