Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

by shaialo (Initiate)
on May 23, 2013 at 20:05 UTC ( #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?


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035017]
Approved by davido
[1nickt]: marto would like to hear that. Of course he speaks very deliberately and dramatically, allowing pauses between words, so if anybody could pull it off it would be him, or someone impersonating him ...
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-09-26 12:17 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (294 votes). Check out past polls.