473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.

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
8 1498
KevinADC
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
I don't understand the before and after data very well. Please provide details.
Apr 3 '08 #9

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

Similar topics

35
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 **********...
4
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
1
by: Jonnie | last post by:
I am in the process of building an employee database for use by the management team here. They current have Access 97 (I am working on getting them to upgrade to at least 2000). For the most part I...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
2
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the...
12
by: Divick | last post by:
Hi all, does any one know of any data structure in which I can search the key and value equally fast? Though I could use hashes, but I can only search in constant time on key but not on value. If...
1
by: npaulus | last post by:
Hi, I am trying to dynamically add user controls on to my web form but for some reason my form isnt displaying the user control. form1.cs: using System; using System.Drawing; using...
1
by: BradenM - Sonoma Computer | last post by:
Hello; I am creating a script that will be taking data from a POST and submitting the data to variables which are placed within a hash for string to data association. Then, using HTML::Table and...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
4
by: Mikey C | last post by:
Hi Everyone, I understand in many cases, adding to history when updating window.location.hash is a GOOD thing. In my case, I want to update bookmarkability from javascript (post-load), but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.