Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Perl Hash

by TechNoFear (Initiate)
on Jul 26, 2005 at 08:30 UTC ( #478091=perlquestion: print w/replies, xml ) Need Help??
TechNoFear has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm currently placing a log record as a string, for example
my $SentRec; open(LOGFILE, "$LogDir$Filename") or return "Error: Open of $Filen +ame failed: $!\n"; while (<LOGFILE>){ if (/\b$Autosys/) { print "the full string: $_\n"; $SentRec = substr($_, 0, 28); $count++; } #sample output 07/25/05 09:03:56 p2.ipgprd.nscc.bti.arch.d Archived BTIHDR.000.137660 +435.Z 07/25/05 09:04:11 p2.ipgprd.nscc.bti.arch.d Archived ML2001788C.137660 +435.Z 07/25/05 09:04:37 p2.ipgprd.nscc.bti.arch.d Archived ML2001789D.137660 +435.Z
and I'm using the timestamp (last one) further on in the program.

What I would like to do is use the first and last time now, so I thought place them into a hash instead, but when I looked at perl hash, I found about 6-8 different types and I'm completly lost as to which variant to use.

I thought I could store them per key and then use the first and last value per key as the two values required, but haven't been able to figure out how to populate the hash yet :(


Replies are listed 'Best First'.
Re: Perl Hash
by davorg (Chancellor) on Jul 26, 2005 at 08:43 UTC

    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.


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

      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 ./ li +ne 183. Execution of ./ 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 :)


        "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.
Re: Perl Hash
by TechNoFear (Initiate) on Jul 26, 2005 at 10:00 UTC
    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.

    p.s. I seem to have got the reply depth too deep !

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

      You can with LoLs (list of lists): perldsc and esp. pay attention to hashs of arrays and hash of hash.

      Update:Seems I replied to the dup

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://478091]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-05-25 10:01 GMT
Find Nodes?
    Voting Booth?