adding data within a hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talaai00
    New Member
    • Mar 2008
    • 11

    #1

    adding data within a hash

    Hi,

    Pardon me if I should have posted this in my previous post, but I feel that this topic is a different discussion. Well, I have successfully pulling what I need from this data file and print them out onto a different file the order that I wanted.

    [CODE=perl] $h_temps{$stn." ".$vol." ".$r1." ".$x1." ".$mag2} = $stn."\t".$vol. "\t".$r1." ".$x1." ".$mag2;
    foreach my $z (sort keys %h_temps)
    {
    print OUTF2 $h_temps{$z}."\ n";
    }[/CODE]

    [CODE=text]T201 U1 0.00073 0.07142 -0.00258
    T201 U1 0.00139 -0.00347 0.00000
    T206 U1 0.00078 0.05879 -0.00356
    T206 U1 0.00103 -0.00283 0.00000
    T206 U2 0.00069 0.05408 -0.00235
    T206 U2 0.00084 -0.00288 0.00000
    T209 U1 0.00070 0.09190 -0.00093
    T209 U1 0.00110 -0.02090 0.00000 [/CODE]

    However, you can also tell that the name ($stn) in the record appears twice with different numbers ($r1,$x1,$mag2) . So what I am attempting to do now is to add those numbers together (in this case just two), which have the same name.

    Desire:
    [CODE=text]T201 U1 0.00869 0.06795 -0.00258
    T206 U1 0.00883 0.05532 -0.00356
    T206 U2 0.00153 0.05120 -0.00235
    T209 U1 0.00180 0.07000 -0.00093 [/CODE]

    Can you guys tell me how I should approach this. I tried putting $r1,$x1,$mag2 into an array and incrementing it, but when all I printed out is the index numbers.

    [CODE=perl]@array[$i] = ($r1,$x1,$mag2) ;
    }
    $i++;[/CODE]
    I am only testing it right now so I don't have a solid code to post up.

    Thanks
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    A hash would be the way to go. If you are not familiar with hashes this is the time to learn.

    Comment

    • talaai00
      New Member
      • Mar 2008
      • 11

      #3
      Well, my posts was kind of long and might be confusing when you got to the end. I have been using hash through out my script and yes I have been learning. I am using hash after hashes to get that data file out. My code is quite long thats why I am not posting all of it, I only post the lines that I think I did wrong or might explain to you guys a little of what i m doing.

      To Kevin, If you think I am trying to get you guys to write the scripe for me without me doing any work, then that is understandable. I need some advices or suggestions of how to accomplish of what I have posted. Other than "learn to use hash" can you give me a little bit more info, I would appreciate. For instance, using hash key, create a matrix, or reference to a hash,etc.

      Thanks

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Personally I am confused by your data and the output. You will need to calrify what ther original data/input is and what the desried final output is.

        Comment

        • talaai00
          New Member
          • Mar 2008
          • 11

          #5
          This is the end of my script where I sorted everything in the hash and print to a file OUTF2.
          [CODE=perl]$h_temps{$stn." ".$vol." ".$r1." ".$x1." ".$mag2} = $stn."\t".$vol. "\t".$r1." ".$x1." ".$mag2;
          foreach my $z (sort keys %h_temps)
          {
          print OUTF2 $h_temps{$z}."\ n";
          }[/CODE]

          The output from this script.
          [CODE=text]T201 U1 0.00073 0.07142 -0.00258
          T201 U1 0.00139 -0.00347 0.00000
          T206 U1 0.00078 0.05879 -0.00356
          T206 U1 0.00103 -0.00283 0.00000
          T206 U2 0.00069 0.05408 -0.00235
          T206 U2 0.00084 -0.00288 0.00000
          T209 U1 0.00070 0.09190 -0.00093
          T209 U1 0.00110 -0.02090 0.00000[/CODE]

          Now from that hash, I want to add the numbers from the last 3 columns where the name on the left column exists twice.

          Desire Output:
          [CODE=text]T201 U1 0.00869 0.06795 -0.00258
          T206 U1 0.00883 0.05532 -0.00356
          T206 U2 0.00153 0.05120 -0.00235
          T209 U1 0.00180 0.07000 -0.00093[/CODE]

          Hope this is not confusing this time.

          Thanks

          Comment

          • talaai00
            New Member
            • Mar 2008
            • 11

            #6
            Ignore my post. I got it partially figure out and I am working to finalize my script at the moment. But if anyone want to add any input, you are welcome to because I might be able to use your input to make my code better.

            Thanks,

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by talaai00
              Ignore my post. I got it partially figure out and I am working to finalize my script at the moment. But if anyone want to add any input, you are welcome to because I might be able to use your input to make my code better.

              Thanks,
              I wouldn't mind trying to help you but all you did was restate what you already stated. I asked for the original data and what you want to end up with. You posted the data that has already been processed by the script you currently have. The code you posted almost looks unecessary for the file output it generates because it is a hash with values the same as the keys but with a tab seperating the various columns.

              Comment

              • talaai00
                New Member
                • Mar 2008
                • 11

                #8
                Originally posted by KevinADC
                I wouldn't mind trying to help you but all you did was restate what you already stated. I asked for the original data and what you want to end up with. You posted the data that has already been processed by the script you currently have. The code you posted almost looks unecessary for the file output it generates because it is a hash with values the same as the keys but with a tab seperating the various columns.
                I know I make this very confusing to figure out what I am trying to do because I cut down a lot on my code and my file do to private matter. This is from a 17mb text file. The pattern of this text file changing constantly at every some hundred lines and has over 170 columns. I understand what you are saying about the hash with values and the keys being the same, but that is the only way I have found that works and generate the order that I wanted. I am sure there are lots of ways to do that, but I tried. You are the expert.

                Imagine this is my original file:
                [CODE=text]T201 U1 0.00073 0.07142 -0.00258
                T201 U1 0.00139 -0.00347 0.00000
                T206 U1 0.00078 0.05879 -0.00356
                T206 U1 0.00103 -0.00283 0.00000
                T206 U2 0.00069 0.05408 -0.00235
                T206 U2 0.00084 -0.00288 0.00000
                T209 U1 0.00070 0.09190 -0.00093
                T209 U1 0.00110 -0.02090 0.00000[/CODE]
                And this is what I want:
                [CODE=text] T201 U1 0.00869 0.06795 -0.00258
                T206 U1 0.00883 0.05532 -0.00356
                T206 U2 0.00153 0.05120 -0.00235
                T209 U1 0.00180 0.07000 -0.00093[/CODE]
                Anyways, we should end our conversation here. I got this figure out already.

                Thanks,

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  I don't understand the before and after data very well. Please provide details.

                  Comment

                  Working...