If I understand correctly, you're wondering what the best data structure for storing the parsed data would be, right? I'd say that depends on what you intend to do with it later on, but as a general rule of thumb, it's usually best to use what most closely resembles the natural structure of the raw data you're reading.
Going by your sample data, this would seem to be an array of hashes, indexed by the event number (0, 1, ...) and then the various fields appearing in these logs. Which is pretty much what you're doing, though you're using a hash with integer keys -- an array in all but name, really.
I want to analize this and report how many instances of errors there are per log type.
That said, I'm not sure what you mean here. What is a "log type"? Assuming you have a variable encoding this, if you merely want aggregates, you could do this in your loop:
... # calculate $logtype $hash_ref->{$logtype}++;
In other words, it all depends on what you want to do.
And bonus question: right now I discard the Description value because it appears on a different line after the ':'. Is there a way of getting this value as well. This is not very important because we have the event id and the logname, so we can deduce the value but it would be nice to have. Thanks!
You should be able to just read from DATA again, like so:
if($_ =~ /\s+Description.*$/) { chomp($description = <DATA>); }
In the event that the description can span several lines, add another loop there that keeps on reading from DATA until it encounters an empty line:
if($_ =~ /\s+Description.*$/) { while(chomp($description_line = <DATA>)) { last if $description_line eq ""; $description .= $description_line } }
Note that none of this is tested in any way.
In reply to Re: question on data structure
by AppleFritter
in thread question on data structure
by natxo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |