hash of a hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cckramer
    New Member
    • Dec 2008
    • 11

    hash of a hash

    I have this info' in a file.

    blockA/input1 to block A/output1 delayXX
    blockA/input1 to blockA/output2 delayYY
    blockA/input1 to blockA/output3 delayZZ

    blockB/input1 to blockB/output1 delayPP

    blockC/input1 to blockC/output1 delayQQ
    blockC/input2 to blockC/output1 delayRR

    how to store the inputs and outputs for each block as shown above ? in 2 hashes or arrays ? I am not interested in the delay shown.I just need to capture the possible combination of inputs and outputs for each block.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    You can probably create a hash with block names as keys. Let the value for each key be an anonymous hash in which inputs are keys and the possible outputs for each input(stored in anonymous array) as values.
    You end up in the following structure which is 'hash of hash of an array'.
    Code:
    %myhash = ( 'blockA' => {'input1'=>['output1','output2']},
                         'blockB' => {'input1'=>['output1']},
                          'blockC' => {'input1'=>['output1'], 'input2'=>['output1']}   ) ;

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Being the late hour that it is I just don't have it in me (mentally) to produce full code at the moment, but what I can say is that you can first use a regular expression to parse out of each line what you are needing as far as text. Then, store it in the hash, as such:

      Code:
      my $text = "blockA/input1 to blockA/output1 delayXX";
      my $hash;
      
      if($text ~= /^(\w)+\/(\w)+\s+\w+\s+(\w)+\/(\w)+\s+(\w)+)
      $hash{$1}{$2} = $5;
      There are a few different ways to handle hashes of hashes, but at work we tend to use something similar to this and it works great. Granted, at this late hour I may have something incorrect in the assignment and also, it is untested, but you should get the idea.

      Also, search google for hash of hashes and you may find some good links.

      Regards,

      Jeff

      Comment

      • cckramer
        New Member
        • Dec 2008
        • 11

        #4
        Thanks

        Thanks. I shall give it a shot..also I tried this rough code.Is this okay ?
        Code:
        # check if line starts with "delay"
         if ($line =~ /^delay/) {
        
        # regular expression to get the inputs & outputs
           $line =~ m/\-from\s+\[\s+(.*)\]\s+\-to\s+\[\s+(.*)\]/;
        
        # inputs & block information
           $in = $1;
           @in1 = split(/\//, $in);
           $blockin = $in1[0];
           $inport = $in1[1];
        
        # outputs & block information
        
           $out = $2;
           @out1 = split(/\//, $out);
           $blockout = $out1[0];
           $outport = $out1[1];
        
        # construct Hash of Hash of Hash function
           $HoHoH{$blockin}{$inport}{$outport} = 1;
        }
        }
        
        
        # check if Hash of Hash of Hash has been correctly stored
        
        # $blocks gives first level
        for my $blocks (keys %HoHoH) {
        
        # inputs gives the second level
          for my $inputs (keys %{$HoHoH{$blocks}}) {
        
        # outputs gives the third level
           for my $outputs (keys %{$HoHoH{$blocks}{$inputs}}) {
           print "^^ $blocks/$inputs -> $blocks/$outputs ^^\n";
        }
        }
        }
        Last edited by numberwhun; Dec 18 '08, 06:12 PM. Reason: Please use code tags

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          You need to please use code tags when posting code. If you notice, the rest of us use them and when you don't, we have to clean up behind you.

          This is your nice warning.

          Regards,

          Jeff

          Comment

          • cckramer
            New Member
            • Dec 2008
            • 11

            #6
            Thank you.I shall keep that in mind for the future

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by cckramer
              Thanks. I shall give it a shot..also I tried this rough code.Is this okay ?
              Code:
              # check if line starts with "delay"
               if ($line =~ /^delay/) {
              
              # regular expression to get the inputs & outputs
                 $line =~ m/\-from\s+\[\s+(.*)\]\s+\-to\s+\[\s+(.*)\]/;
              
              # inputs & block information
                 $in = $1;
                 @in1 = split(/\//, $in);
                 $blockin = $in1[0];
                 $inport = $in1[1];
              
              # outputs & block information
              
                 $out = $2;
                 @out1 = split(/\//, $out);
                 $blockout = $out1[0];
                 $outport = $out1[1];
              
              # construct Hash of Hash of Hash function
                 $HoHoH{$blockin}{$inport}{$outport} = 1;
              }
              }
              
              
              # check if Hash of Hash of Hash has been correctly stored
              
              # $blocks gives first level
              for my $blocks (keys %HoHoH) {
              
              # inputs gives the second level
                for my $inputs (keys %{$HoHoH{$blocks}}) {
              
              # outputs gives the third level
                 for my $outputs (keys %{$HoHoH{$blocks}{$inputs}}) {
                 print "^^ $blocks/$inputs -> $blocks/$outputs ^^\n";
              }
              }
              }

              Hard to say because the data your code is parsing is not the same as the sample data you posted. On the face of it, the code looks OK to me. But I think a hash of arrays might work better. Whats the actual data look like you are parsing?

              Comment

              • cckramer
                New Member
                • Dec 2008
                • 11

                #8
                Thank for all those who helped.My code that I posted earlier to create a hash of hash of hash worked.
                Code:
                (keys{$ blocks{$inputs}{$outputs}})
                Last edited by numberwhun; Dec 19 '08, 03:08 PM. Reason: Please use code tags

                Comment

                Working...