in reply to Perl Hash

Not sure where you found "6-8 different types" of hash. There's only one. The definitive documentation is in the perldata man page that came with your Perl distribution.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Perl Hash
by TechNoFear (Initiate) on Jul 26, 2005 at 09:05 UTC
    thanks Dave, well I think I'm populating the hash now, but cannot prove it as I have a problem with the print loop
    my $HashKey = $_[4]; $HashKey += 1; # initialise the hash my %LogHash = (); while (<LOGFILE>){ if (/\b$Autosys/) { # populate the hash %LogHash = ($HashKey => $SentRec = substr($_, 0, 20)); } } # debugging stub # print out the contents of the hash foreach $k (keys(%LogHash)) { print LOGFILE $k, ",", $LogHash{$k}, "\n"; }
    the error is
    Global symbol "k" requires explicit package name at ./pmc_tester.pl li +ne 183. Execution of ./pmc_tester.pl aborted due to compilation errors.
    So I'm not there yet...

      You've obviously got "use strict" in your program (which is a good habit to get into) so you need to predeclare[1] all of your variables.

      foreach my $k (keys(%LogHash)) { print LOGFILE $k, ",", $LogHash{$k}, "\n"; }

      Oh, but there's no need to make that print statement so complex.

      foreach my $k (keys(%LogHash)) { print LOGFILE "$k, $LogHash{$k}\n"; }

      [1] Or fully qualify their names, but let's not get into that :)

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      I'm afraid this whole script is a bit confused. You really should read up on hashes. There are much better explanations available than I could give here.

      Anyway, with that said, you get an error because you've not delcared $k in your foreach loop ala...

      foreach my $k (keys(%LogHash)) { print LOGFILE $k, ",", $LogHash{$k}, "\n"; }
      Your also emptying your hash with the line...
      %LogHash = ($HashKey => $SentRec = substr($_, 0, 20));
      assigning a key with a value would normally be done like this
      $LogHash{%HashKey} = "THEVALUE";
      ... but this isn't going to help you much because I'm still not sure what the output of the program should be.

      You also seem to think that you can have multiple values for a single key. That's not true, hashes are one key -> one value (that's not to say the value couldn't be an array reference, but that's a whole other topic).

      you might also want to take a look at data::dumper, which is great for debugging hashes (by printing them)

      use Data::Dumper; print Dumper(\%hash);
      good luck
      ---
      my name's not Keith, and I'm not reasonable.
        thanks Dave, I've got past the "not declaring my $k further up the program" problem now :)

        but it appears my problem is what "not Keith" says

        >> You also seem to think that you can have multiple values for a single key. That's not true,

        well in that case, I do have a problem as I want to store several lines of text against the same key like this



        KEY1 07/25/05 09:04:36 KEY2 07/25/05 09:04:36 KEY2 07/25/05 09:04:37 KEY2 07/25/05 09:05:49 KEY2 07/25/05 09:05:50 KEY3 07/25/05 09:05:50


        so then I can find the earliest time in KEY2 is 09:04:36 and the latest time is 09:05:50

        However, now I'll have to find another way.

        Thanks for your help.