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

Hey Monks, I have an issue you can probably maybe help me with. I have a piece of code that seems not being working for just one random value... As we all know that random results and programming are barely compatible, I am seeking advices to help me troubleshoot the code bellow.

%ulm_msg : just raw ULM messages. Example in the output bellow.

@ulm_tags : contain MSGTYPE GCCI.PRODUCT.FLOW EXECSERVPRODUCT

All the others values are initialized in the code bellow:

------------------------------------

# Creation of a hash of anonymous hash # Model is : KeyTag => { KeyVal => Frequency } # Example: 35 => { D => 10 } # 35 => { G => 3 } foreach $h_tag (@ulm_tags){ $ulm_keys{$h_tag}{"FAKE"} = "FAKE"; # [ULM TAGS]: [ MSGTYPE | GCCI.PRODUCT.FLOW | EXECSERV +PRODUCT ] # %ULM KEYS: GCCI.PRODUCT.FLOW => { 1 => 1 } # %ULM KEYS: MSGTYPE => { 1 => 1 } } # Populating the hash of anonymous hash while (($key, $value) = each(%ulm_msg)) { foreach $h_tag (keys %ulm_keys) { if($value =~ /$h_tag/){ $value =~ /\|$h_tag=([ +^|]*)/; $h_val = $1; printf "\nh_tag:h_val +=> ".$h_tag.":".$h_val."\n"; if(exists $ulm_keys{$h +_tag}{$h_val}){ $ulm_k +eys{$h_tag}{$h_val} => $ulm_keys{$h_tag}{$h_val}++; printf + "Incrementing a hash entry: ".$h_tag.":".$h_val."=>".$ulm_keys{$h_ta +g}{$h_val}."\n"; } else{ $ulm_k +eys{ $h_tag } = { + $h_val => 1, }; printf + "Adding a new hash entry: ".$h_tag.":".$h_val."=>".$ulm_keys{$h_tag} +{$h_val}."\n"; if($h_ +val eq "AQUA"){ + printf "ULM: ".$value."\n"; } } } } } # Display the Hash printf "\n\nHASH OF HASH\n\n"; for $h_tag ( keys %ulm_keys ) { print "$h_tag: "; for $h_val ( keys %{ $ulm_keys{$h_tag} } ) { print "$h_val=$ulm_keys{$h_tag}{$h_va +l} "; } print "\n"; }

------------------------------------

So this piece of code seems to be absolutely fine. Except one thing, for just one value the line : if(exists $ulm_keys{$h_tag}{$h_val}){....

Seems to not be true, meaning we are recreating an existing key/pair. I do not understand why. Bellow an extract of the output, that can help to spot the issue and understand the content of each variable:

h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>12 h_tag:h_val => EXECSERVPRODUCT:ARID Adding a new hash entry: EXECSERVPRODUCT:ARID=>1 h_tag:h_val => GCCI.PRODUCT.FLOW:ATS Incrementing a hash entry: GCCI.PRODUCT.FLOW:ATS=>13 h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>13 h_tag:h_val => EXECSERVPRODUCT:TWAP Adding a new hash entry: EXECSERVPRODUCT:TWAP=>1 h_tag:h_val => GCCI.PRODUCT.FLOW:ATS Incrementing a hash entry: GCCI.PRODUCT.FLOW:ATS=>14 h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>14 h_tag:h_val => EXECSERVPRODUCT:CLOSE Adding a new hash entry: EXECSERVPRODUCT:CLOSE=>1 h_tag:h_val => GCCI.PRODUCT.FLOW:ATS Incrementing a hash entry: GCCI.PRODUCT.FLOW:ATS=>15 h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>15 h_tag:h_val => EXECSERVPRODUCT:AQUA Adding a new hash entry: EXECSERVPRODUCT:AQUA=>1 ULM: 8=FIX.4.2|9=898|35=UL|49=EMC01|56=EMSECS|34=6007|52=20120221-09:5 +9:47|212=831|#EXDESTINATION=L|#GCCI.PRODUCT.EXECUTION_REGION=EMEA|#GC +CI.PRODUCT.EXECUTION_SUB_REGION=EMEA|#IDSOURCE=5|#ONBEHALFOFCOMPID=P2 +|#SECURITYID=VOD.L|#SYMBOL=VOD.L|CLORDID=GGPHCJP9459dd|CREATIONTIME=2 +0120221095947|EXDESTINATION=L|EXECSERVPRODUCT=AQUA|GCCI.CLIENT.COMPID +=P2|GCCI.CLIENT.FIXSESSION=TEST_|GCCI.INSTRUMENT.REUTERS_EXCHANGE=L|G +CCI.PRODUCT.EXECUTION_REGION=EMEA|GCCI.PRODUCT.EXECUTION_SUB_REGION=E +MEA|GCCI.PRODUCT.FLOW=ATS|HANDLINST=care|MSGTYPE=new|ONBEHALFOFCOMPID +=P2|ORDERQTY=1|ORDSTATUS=pendingnew|ORDTYPE=market|PLUGINORIGINATOR=T +EST__GEN|PLUGINOWNER=TEST__GEN|RICCODE=VOD.L|ROOTCLIENTORDERID=JP9459 +dd|SENDERCOMPID=TEST_|SIDE=buy|SYMBOL=VOD.L|TARGETCOMPID=_|TIMEINFORC +E=day|TRANSACTTIME=20120221095945806|ULFROMSESSIONNAME=TEST__GEN|ULTO +SESSIONNAME=PULM_EMC01_EMSECS||10=219| h_tag:h_val => GCCI.PRODUCT.FLOW:ATS Incrementing a hash entry: GCCI.PRODUCT.FLOW:ATS=>16 h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>16 h_tag:h_val => EXECSERVPRODUCT:TWAP Adding a new hash entry: EXECSERVPRODUCT:TWAP=>1 h_tag:h_val => GCCI.PRODUCT.FLOW:ATS Incrementing a hash entry: GCCI.PRODUCT.FLOW:ATS=>17 h_tag:h_val => MSGTYPE:new Incrementing a hash entry: MSGTYPE:new=>17 h_tag:h_val => EXECSERVPRODUCT:AQUA Adding a new hash entry: EXECSERVPRODUCT:AQUA=>1 ULM: 8=FIX.4.2|9=898|35=UL|49=EMC01|56=EMSECS|34=6009|52=20120221-09:5 +9:47|212=831|#EXDESTINATION=L|#GCCI.PRODUCT.EXECUTION_REGION=EMEA|#GC +CI.PRODUCT.EXECUTION_SUB_REGION=EMEA|#IDSOURCE=5|#ONBEHALFOFCOMPID=_P +4|#SECURITYID=VOD.L|#SYMBOL=VOD.L|CLORDID=GGPHCJP9461dd|CREATIONTIME= +20120221095947|EXDESTINATION=L|EXECSERVPRODUCT=AQUA|GCCI.CLIENT.COMPI +D=_P4|GCCI.CLIENT.FIXSESSION=TEST_|GCCI.INSTRUMENT.REUTERS_EXCHANGE=L +|GCCI.PRODUCT.EXECUTION_REGION=EMEA|GCCI.PRODUCT.EXECUTION_SUB_REGION +=EMEA|GCCI.PRODUCT.FLOW=ATS|HANDLINST=care|MSGTYPE=new|ONBEHALFOFCOMP +ID=_P4|ORDERQTY=1|ORDSTATUS=pendingnew|ORDTYPE=market|PLUGINORIGINATO +R=TEST__GEN|PLUGINOWNER=TEST__GEN|RICCODE=VOD.L|ROOTCLIENTORDERID=JP9 +461dd|SENDERCOMPID=TEST_|SIDE=buy|SYMBOL=VOD.L|TARGETCOMPID=|TIMEINFO +RCE=day|TRANSACTTIME=20120221095945806|ULFROMSESSIONNAME=TEST__GEN|UL +TOSESSIONNAME=PULM_EMC01_EMSECS||10=213| HASH OF HASH! GCCI.PRODUCT.FLOW: ATS=17 MSGTYPE: new=17 EXECSERVPRODUCT: AQUA=1
---------------------------------------------------------

As you can see the key/pair => EXECSERVPRODUCT:AQUA is added more than one time. All the others key/pairs seems to be fine. Any idea why ?

Replies are listed 'Best First'.
Re: Hash of hash check if defined
by davido (Cardinal) on Feb 21, 2012 at 16:20 UTC

    Autovivification.

    Change...

    if(exists $ulm_keys{$h_tag}{$h_val}){....

    ... to ...

    if( exists $ulm_keys{$h_tag} and exists $ulm_keys{$h_tag}{$h_val} ) { ...

    ...and see if that fixes things. What's happening is that currently as you test for the existance of $h_val, $h_tag is created automatically whether or not $h_val exists.

    Here is Uri Gutman's tutorial on the subject.


    Dave

      Hi Dave thanks for your quick answer. I don't think this is an autovivification issue as I've already try the following without any success :

      if(exists $ulm_keys{$h_tag} && defined $ulm_keys{$h_tag}{$h_val}){

      But I will try to check with 2 exists, just in case. Thanks again for your answer.

Re: Hash of hash check if defined
by Jup (Novice) on Feb 21, 2012 at 17:13 UTC
    Issue solved, that was a reference issue. Thanks to those who had a look into it. FYI:

    Replace

    $ulm_keys{ $h_tag } = { $h_val => 1, };

    With

    $ulm_keys{$h_tag}{$h_val}=1;