Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to create a non uniform multi-dimention array with push?

by shaialo (Initiate)
on May 23, 2013 at 20:05 UTC ( [id://1035017]=perlquestion: print w/replies, xml ) Need Help??

shaialo has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create a non uniform multi-dimention array with push.

The purpose is to create an array holding a histogram of error lines in a component log file.

For example:

Say I have a LOGFILE that I need to filer only the error messages out of it, but divide the times into groups, like: if the LOGFILE contain entries of 2 hours from 00:00:00 till 02:00:00, I need to check in every 10 min in that time period, how many error messages appeared and from which type.

Therefore, for example I have 4 error types in an array, like "File not found", "File is empty", "Wrong permissions", "object is not a file", respectively as 0..4 .

I'm trying to create the following structure:

+------------------+---------------+------------------+ | Error Type index | Time interval | Number of errors | +------------------+---------------+------------------+ | 0 | 00:00:00 | 1 | | | 00:10:00 | 5 | | | .... | .... | | | 01:50:00 | 7 | +------------------+---------------+------------------+ | 1 | 00:00:00 | 21 | | | 00:10:00 | 34 | | | .... | .... | | | 01:50:00 | 11 | +------------------+---------------+------------------+ | | .... | .... | +------------------+---------------+------------------+ | 3 | 00:00:00 | 0 | | | 00:10:00 | 0 | | | .... | .... | | | 01:50:00 | 17 | +------------------+---------------+------------------+

In this way, the 1 column size is 4, the 2nd and 3rd columns sizes are 12.

I'm filling the data when reading the LOGFILE line by line and consider that this array doesn't exist and I need to create it using push.

Is there any effective way of doing this in the right way?

Thanks

Replies are listed 'Best First'.
Re: How to create a non uniform multi-dimention array with push?
by RichardK (Parson) on May 24, 2013 at 11:48 UTC

    perldsc Perl Data Structures Cookbook, has lots of useful information about how to create complex data structures. It's well worth a read, and should give you lots of ideas about how to solve your problem.

Re: How to create a non uniform multi-dimention array with push?
by state-o-dis-array (Hermit) on May 23, 2013 at 21:10 UTC
    I'm not sure I'm clear on all the requirements, but if the time intervals are known and consistent for each index, meaning you won't need to store them, you could:
    push( @{$log[$error_type_index]}, $error_count );
Re: How to create a non uniform multi-dimention array with push?
by hdb (Monsignor) on May 24, 2013 at 09:27 UTC

    I would usually use a hash for such a task. Have a look at the following pseudo code:

    my %report; for my $line (<LOGFILE>) { ... # extract error type and time from $line here ... $report{$errortype}{...time rounded to the previous 10 mins...}++; } # print table here
Re: How to create a non uniform multi-dimention array with push?
by Cristoforo (Curate) on May 24, 2013 at 23:11 UTC
    Could you post 20 or so lines of the log file (within code tags)?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1035017]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found