Hash search

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

    Hash search

    [code=perl]
    foreach my $x (sort keys %h_temp)
    {

    print OUTF2 $h_temp{$x};
    chomp;
    $data = substr($_,43,4) ;
    $data2 = substr($_,54,4) ;
    $data =~ s/\s+$//;
    $data2 =~ s/\s+$//;

    if($data eq " 601")
    {
    $mix = trim(substr($_, 25,17));
    $h_loads{$mix} = $_;
    }
    if($data2 eq " 601")
    {
    $mix2 = trim(substr($_, 25,17));
    $h_loads{$mix2} = $_;
    }

    }
    foreach my $y (sort keys %h_loads)
    {
    print OUTF $h_loads{$y};
    }

    [/code]

    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

    Code:
    'PAX MIT8    ', 645, 645,1.03375, -65.7583, 601
    'PCHBTM 2    ',  25,   1,1.01000, -62.5354, 601
    'ASH GRV8    ','O0',1, 645, 645,   15.830,   11.370, 601
    'AUBURN 8    ','N0',1, 645, 640,     8.100,     1.900, 601
    and when I attemp to pull these column

    Code:
    1.03375, -65.7583
    1.01000, -62.5354
    15.830,   11.370
    8.100,     1.900
    it wouldn't print anything. Can someone show me what I did wrong?

    Thank you,

    TA
    Last edited by eWish; Mar 28 '08, 12:30 AM. Reason: Fixed code tags
  • talaai00
    New Member
    • Mar 2008
    • 11

    #2
    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.

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      When you are checking for equality of a number you need to use '==' in lieu of 'eq'.

      Change:
      Code:
      if($data eq " 601")
      if($data2 eq " 601")
      To:
      Code:
      if($data == 601 )
      if($data2 == 601 )
      See that fixes your problem.

      --Kevin

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        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?
        Code:
        $data = substr($_,43,4);
        should that be some other variable, like a hash key?

        Comment

        • talaai00
          New Member
          • Mar 2008
          • 11

          #5
          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.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by talaai00
            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.

            Comment

            • talaai00
              New Member
              • Mar 2008
              • 11

              #7
              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:
              Code:
              999IN  8 1
              HAS GRV8 ,    15.830,    11.37
              BRNTIN 8 1
              CLASS  G ,     1.300,     0.80
              CLASS  G ,     1.300,     0.80
              FC1A   5 1
              FC1A1  9 ,     6.200,     3.50
              FC1A2  9 ,     4.400,     2.90
              FC1A3  9 ,     3.200,     2.00
              [CODE=perl]foreach my $y (sort keys %h_loads)
              {
              print OUTF $y." ".$h_loads{$y}. "\n"
              }[/CODE]

              This is almost what I wanted, but I don't want to print out names that don't have numbers behind them.

              Desire:
              Code:
              HAS GRV8 ,    15.830,    11.37
              CLASS  G ,     1.300,     0.80
              CLASS  G ,     1.300,     0.80
              FC1A1  9 ,     6.200,     3.50
              FC1A2  9 ,     4.400,     2.90
              FC1A3  9 ,     3.200,     2.00
              I tried print if and print unless, but no result.

              [CODE=perl]print OUTF $y." ".$h_loads{$y}. "\n" unless $h_loads{$y} =~ / 1 /;[/CODE]

              [CODE=perl]print OUTF $y." ".$h_loads{$y}. "\n" if $h_loads{$y} !~ / 1 /;
              [/CODE]

              Can you please tell me what I did wrong here, thanks for your help.

              Comment

              • talaai00
                New Member
                • Mar 2008
                • 11

                #8
                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

                [CODE=perl]print OUTF $y." ".$h_loads{$y}. "\n" unless $h_loads{$y} == " 1 ";[/CODE]

                Comment

                Working...