------------------------------------------------ On Mon, 4 Aug 2003 17:09:40 -0400 , Smith Jeff D <[EMAIL PROTECTED]> wrote:
> This is a request for insight on structuring data from a log file for later > retrieval and stuffing into a mail message. > I'm having trouble trying to set up the proper form of references to store > this data. Here's the source log file format(line numbers are shown for > clarification): > > Line1: StatusField Server Name Field TimeStamp OtherStuff > Line2 SUCCESS server01 Today's date Other details... > Line3 WARNING server22 Today's date Other details...: > Line4 SUCCESS server22 Today's date Still other > details... > Line5 WARNING server55 Today's date Other > details.... > Line6 WARNING server01 Today's date Other > details.... > > .... > > And so on > > Each line can have the same server, timestamp and details are another line > with the same server name--there are no complete duplicates of any line but > lines could > Have the same server name and timestamp but differ on the status field for > certain detail items. > > I want to associate all lines from a particular server together so that I > can mail copies of the lines in the log reports to particular addresses > based on reading another file that contains a servername-->mailaddress > association. > > I thought that I would read all the lines into an array, sort them by the > server name so that all the records (lines) for a particular server are > next to each other, loop through the records one by one, splitting each line > into its individual columns, reading the other file of > servername->mailaddresses to grab the mailaddress and after the servername > changes, send the array of lines to a mailer. I can do the latter part fine > if I only have one line per server. But since there can be multiple lines > per server, I'm stumped how to set the data structure up. > > What I'm having a problem with is figuring out how to store the lines for a > particular server in a structure that I can use as the basis of the mail > message--I thought I wanted a hash of array references but can't figure out > to do that without having a hard reference to an array for each server > (that'd require hundreds of names and it appears to be a stupid approach to > associate the structure with the values in the data. I think I want some > form of anonymous array that can be referenced by a hash based on servername > but can't seem to figure out how to get it set up correctly. It must be a > mental block on the correct form of hash of arrays where the hash key is the > servername and the value is an array of all arrays/records that contain the > servername but... > > Sounds like you have a decent approach, I like your second idea better than storing everything to an array and then sorting by server name. Your hash idea will take care of that for you and you probably want to avoid the double memory usage of storing all data to an array first, and avoid the CPU intensive operation of sorting (not to mention you lose your date/time sequence)....so having a look at your second option, something along the lines of: my $servers = {}; while (my $line = <READLOG>) { my @line = split(/ /, $line); push @{$servers->{$line[1]}}, $line[2]; } At this point you have a hash reference where the keys are the server names and the value is an array (reference) of lines from the log file. The key is to recognize I start out with $servers being a hash reference. Then for each line of the file I append (push) to an array reference stored as the value of the hash key. Then to access the contents of the servers: while (my ($server, $array) = each(%$servers)) { # send message where $server is the server name # and $array is an array reference of log lines my $to = $config{$server}; my @body = @$array; } This is just pseudo-code that you will need to flesh out with what you actually want and message code, etc. But it should give you idea of how to setup the structure. The following might help as well if you haven't already read them: perldoc perlreftut perldoc perlref perldoc perllol perldoc perldsc And you may want to check out the Data::Dumper module to help you debug. It allows you to print a data structure in readable format to see if you really have what you want (though I suggest starting with a small sample of data in your case). perldoc Data::Dumper Then come back when you get frustrated or hit a snag. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]