473,383 Members | 1,868 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,383 software developers and data experts.

Hash search

Expand|Select|Wrap|Line Numbers
  1. foreach my $x (sort keys %h_temp) 
  2.     {
  3.  
  4.         print OUTF2 $h_temp{$x};
  5.         chomp;
  6.         $data = substr($_,43,4);
  7.     $data2 = substr($_,54,4);
  8.         $data =~ s/\s+$//;
  9.     $data2 =~ s/\s+$//;
  10.  
  11.         if($data eq " 601")
  12.         {
  13.             $mix = trim(substr($_,25,17));
  14.             $h_loads{$mix} = $_;
  15.         }
  16.         if($data2 eq " 601")
  17.         {
  18.             $mix2 = trim(substr($_,25,17));
  19.             $h_loads{$mix2} = $_;
  20.         }    
  21.  
  22.      }                 
  23. foreach my $y (sort keys %h_loads) 
  24.     {
  25.         print OUTF $h_loads{$y};
  26.     }      
  27.  
  28.  
Hi, I am having problem searching through my hash and pulling out specific column that matches my search. My hash $h_temp printed this out

Expand|Select|Wrap|Line Numbers
  1. 'PAX MIT8    ', 645, 645,1.03375, -65.7583, 601
  2. 'PCHBTM 2    ',  25,   1,1.01000, -62.5354, 601
  3. 'ASH GRV8    ','O0',1, 645, 645,   15.830,   11.370, 601
  4. 'AUBURN 8    ','N0',1, 645, 640,     8.100,     1.900, 601
and when I attemp to pull these column

Expand|Select|Wrap|Line Numbers
  1. 1.03375, -65.7583
  2. 1.01000, -62.5354
  3. 15.830,   11.370
  4. 8.100,     1.900
it wouldn't print anything. Can someone show me what I did wrong?

Thank you,

TA
Mar 27 '08 #1
7 1307
The 1st and 2nd row records in $h_temp should line up and 3rd and 4th should be lining up also. Sorry about that, I thought I lined them up before I posted.
Mar 27 '08 #2
eWish
971 Expert 512MB
When you are checking for equality of a number you need to use '==' in lieu of 'eq'.

Change:
Expand|Select|Wrap|Line Numbers
  1. if($data eq " 601")
  2. if($data2 eq " 601")
To:
Expand|Select|Wrap|Line Numbers
  1. if($data == 601 )
  2. if($data2 == 601 )
See that fixes your problem.

--Kevin
Mar 28 '08 #3
KevinADC
4,059 Expert 2GB
There is no place in your code where you are extracting data from a hash that I can see. Why are you using $_ in the substr() function?
Expand|Select|Wrap|Line Numbers
  1. $data = substr($_,43,4);
should that be some other variable, like a hash key?
Mar 28 '08 #4
Thanks eWish for cleaning up my post and I will try it your way.

To KevinADC, the reason I use $_ in the substr() function because it seems to work when I loop through a text file. So, I thought looping through a hash is the same way. I am quite new to Perl so I got a lot of things to learn from you guys and books I got laying on the table.

This is actually two different code that I wrote and test them separately looping through the same text file. The file was very large so I tried to combine the code together and narrowing the search parameter.

Thanks guys, I will try your advice.
Mar 28 '08 #5
KevinADC
4,059 Expert 2GB
Thanks eWish for cleaning up my post and I will try it your way.

To KevinADC, the reason I use $_ in the substr() function because it seems to work when I loop through a text file. So, I thought looping through a hash is the same way. I am quite new to Perl so I got a lot of things to learn from you guys and books I got laying on the table.

This is actually two different code that I wrote and test them separately looping through the same text file. The file was very large so I tried to combine the code together and narrowing the search parameter.

Thanks guys, I will try your advice.
OK, I see. $_ is perls default scalar, you can use it for many things but not for what you are trying to do. Well, you could, but it would be senseless. Just use the appropriate hash key to extract the data from and make any other changes that have been mentioned. Post back if you need more help.
Mar 28 '08 #6
Hello again,

I tried to do what you guys suggested, but It wouldn't print. So, I went a different rout where I looped through the text file for a certain pattern and save it in a hash. Then I looped through the same file for a different pattern and save it in a different hash. I also cleaned up my code so it is no longer given me warnings; therefore response much faster.

So here is the result of my code:
Expand|Select|Wrap|Line Numbers
  1. 999IN  8 1
  2. HAS GRV8 ,    15.830,    11.37
  3. BRNTIN 8 1
  4. CLASS  G ,     1.300,     0.80
  5. CLASS  G ,     1.300,     0.80
  6. FC1A   5 1
  7. FC1A1  9 ,     6.200,     3.50
  8. FC1A2  9 ,     4.400,     2.90
  9. FC1A3  9 ,     3.200,     2.00
Expand|Select|Wrap|Line Numbers
  1. foreach my $y (sort keys %h_loads) 
  2.     {
  3.             print OUTF $y." ".$h_loads{$y}."\n"
  4.     }
This is almost what I wanted, but I don't want to print out names that don't have numbers behind them.

Desire:
Expand|Select|Wrap|Line Numbers
  1. HAS GRV8 ,    15.830,    11.37
  2. CLASS  G ,     1.300,     0.80
  3. CLASS  G ,     1.300,     0.80
  4. FC1A1  9 ,     6.200,     3.50
  5. FC1A2  9 ,     4.400,     2.90
  6. FC1A3  9 ,     3.200,     2.00
I tried print if and print unless, but no result.

Expand|Select|Wrap|Line Numbers
  1. print OUTF $y." ".$h_loads{$y}."\n" unless $h_loads{$y} =~ / 1  /;
Expand|Select|Wrap|Line Numbers
  1. print OUTF $y." ".$h_loads{$y}."\n" if $h_loads{$y} !~ / 1  /;
  2.  
Can you please tell me what I did wrong here, thanks for your help.
Mar 31 '08 #7
Woot, I got it to work now and it printed like I wanted it to. I suddenly remember that Kevin (eWish) said use "==" for equality of a number and it work for me in this case. Thanks

Expand|Select|Wrap|Line Numbers
  1. print OUTF $y." ".$h_loads{$y}."\n" unless $h_loads{$y} == " 1   ";
Mar 31 '08 #8

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

Similar topics

34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
4
by: Thomas Christmann | last post by:
Hi! First let me apologize for asking this question when there are so many answers to it on Google, but most of them are really contradicting, and making what I want to do very performant is...
7
by: Zhiqiang Ye | last post by:
Hi,dear all, I know there is a hash library in glibc, it's head file is search.h. One can use hash map based on this.But its hash is global , there could be only one hash structrue at one time. Is...
1
by: Sergei Koveshnikov | last post by:
Hello, I am trying to use hash management (from <search.h>). I can add and search however I can't update entries in the hash table... In the manual I found following note: ============ man (3)...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
0
by: fake ID | last post by:
Since you can't search for these symbols used in asp.net "<%#" or '<%=' I thought i'd post this to make things a little easier to find. Potential search word combinations: -lessthan Percentage...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.