Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl-Sensitive Sunglasses
 
PerlMonks  

Algorithmic hash bug.

by thenetfreaker (Friar)
on Feb 19, 2007 at 07:53 UTC ( #600788=perlquestion: print w/ replies, xml ) Need Help??
thenetfreaker has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm tring to put into my variable $x( for instance,) the $value of a key in a hash where the mainKey equals $valueM, and all i could think of how it would look "without a singl loop" is :
$hash{'mainKey'}{$valueM}{'key'}
So how can i correct my mistake ?
Thanks in advance.

Comment on Algorithmic hash bug.
Download Code
Re: Algorithmic hash bug.
by shmem (Canon) on Feb 19, 2007 at 08:24 UTC
    "without a singl loop"

    Direct hash lookups indeed work without loops.

    So how can i correct my mistake ?

    which mistake? Got some error? Some code, apart from that multilevel hash lookup?

    From what you say I figure you mean

    my $x = $hash{$valueM}{$key};

    because you say "where the mainKey equals $valueM". As you have written that multilevel hash lookup, the string contained in $valueM would be one key in an anonymous hash stored as the value for the key 'mainKey' in the hash %hash. I guess that's different to what you wrote. But how can I know? I know what I mean. Why don't you?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      and what if $valueM if from some third party parameterKey ?

      p.s.: How can i sort a hash by a spesific key ?

        Show us 10 lines of code that run under use strict; use warnings and demonstrate the problem you are having. Show us the result you have currently and what you desire. You may find it helpful to read I know what I mean. Why don't you?.


        DWIM is Perl's answer to Gödel
Re: Algorithmic hash bug.
by ysth (Canon) on Feb 19, 2007 at 10:20 UTC
    "without a singl loop"
    What are you quoting? A homework assignment?
Re: Algorithmic hash bug.
by cdarke (Prior) on Feb 19, 2007 at 13:19 UTC
    If I read your question correctly, it is very simple:
    $x = $hash{$valuem};
    The solution you provided implies: a hash with the key 'mainKey', whose value is a reference to a hash with the key in $valueM, whose value is a reference to a hash with the key 'key'.
Re: Algorithmic hash bug.
by pajout (Curate) on Feb 19, 2007 at 15:24 UTC
    I guess that you want something like this:
    use strict; use warnings; my $valueM = 'myvalue'; my $hashref; $$hashref{mainKey}{$valueM}{key} = 'value';

    or
    my %hash; $hash{key} = 'value'; ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://600788]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-04-18 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls