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

Hash search

P: 11
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
Share this Question
Share on Google+
7 Replies


P: 11
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
Expert 100+
P: 971
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
Expert 2.5K+
P: 4,059
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

P: 11
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
Expert 2.5K+
P: 4,059
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

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

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

Post your reply

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