By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,276 Members | 1,532 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,276 IT Pros & Developers. It's quick & easy.

adding data within a hash

P: 11
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.

Expand|Select|Wrap|Line Numbers
  1.  $h_temps{$stn." ".$vol." ".$r1." ".$x1." ".$mag2} = $stn."\t".$vol."\t".$r1." ".$x1." ".$mag2;
  2. foreach my $z (sort keys %h_temps)
  3.     {
  4.         print OUTF2 $h_temps{$z}."\n";
  5.     }
Expand|Select|Wrap|Line Numbers
  1. T201    U1    0.00073 0.07142 -0.00258
  2. T201    U1    0.00139 -0.00347 0.00000
  3. T206    U1    0.00078 0.05879 -0.00356
  4. T206    U1    0.00103 -0.00283 0.00000
  5. T206    U2    0.00069 0.05408 -0.00235
  6. T206    U2    0.00084 -0.00288 0.00000
  7. T209    U1    0.00070 0.09190 -0.00093
  8. T209    U1    0.00110 -0.02090 0.00000 
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:
Expand|Select|Wrap|Line Numbers
  1. T201    U1    0.00869 0.06795 -0.00258
  2. T206    U1    0.00883 0.05532 -0.00356
  3. T206    U2    0.00153 0.05120 -0.00235
  4. T209    U1    0.00180 0.07000 -0.00093  
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.

Expand|Select|Wrap|Line Numbers
  1. @array[$i] = ($r1,$x1,$mag2);
  2.         }
  3.     $i++;
I am only testing it right now so I don't have a solid code to post up.

Thanks
Apr 1 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
A hash would be the way to go. If you are not familiar with hashes this is the time to learn.
Apr 1 '08 #2

P: 11
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
Apr 2 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
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.
Apr 2 '08 #4

P: 11
This is the end of my script where I sorted everything in the hash and print to a file OUTF2.
Expand|Select|Wrap|Line Numbers
  1. $h_temps{$stn." ".$vol." ".$r1." ".$x1." ".$mag2} = $stn."\t".$vol."\t".$r1." ".$x1." ".$mag2;
  2. foreach my $z (sort keys %h_temps)
  3.     {
  4.         print OUTF2 $h_temps{$z}."\n";
  5.     }
The output from this script.
Expand|Select|Wrap|Line Numbers
  1. T201    U1  0.00073 0.07142 -0.00258
  2. T201    U1  0.00139 -0.00347 0.00000
  3. T206    U1  0.00078 0.05879 -0.00356
  4. T206    U1  0.00103 -0.00283 0.00000
  5. T206    U2  0.00069 0.05408 -0.00235
  6. T206    U2  0.00084 -0.00288 0.00000
  7. T209    U1  0.00070 0.09190 -0.00093
  8. T209    U1  0.00110 -0.02090 0.00000
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:
Expand|Select|Wrap|Line Numbers
  1. T201    U1  0.00869 0.06795 -0.00258
  2. T206    U1  0.00883 0.05532 -0.00356
  3. T206    U2  0.00153 0.05120 -0.00235
  4. T209    U1  0.00180 0.07000 -0.00093
Hope this is not confusing this time.

Thanks
Apr 2 '08 #5

P: 11
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,
Apr 2 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
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.
Apr 2 '08 #7

P: 11
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:
Expand|Select|Wrap|Line Numbers
  1. T201    U1  0.00073 0.07142 -0.00258
  2.       T201    U1  0.00139 -0.00347 0.00000
  3.       T206    U1  0.00078 0.05879 -0.00356
  4.       T206    U1  0.00103 -0.00283 0.00000
  5.       T206    U2  0.00069 0.05408 -0.00235
  6.       T206    U2  0.00084 -0.00288 0.00000
  7.       T209    U1  0.00070 0.09190 -0.00093
  8.       T209    U1  0.00110 -0.02090 0.00000
And this is what I want:
Expand|Select|Wrap|Line Numbers
  1.  T201    U1  0.00869 0.06795 -0.00258
  2.       T206    U1  0.00883 0.05532 -0.00356
  3.       T206    U2  0.00153 0.05120 -0.00235
  4.       T209    U1  0.00180 0.07000 -0.00093
Anyways, we should end our conversation here. I got this figure out already.

Thanks,
Apr 3 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
I don't understand the before and after data very well. Please provide details.
Apr 3 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.