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

Dear Perl-Monks


I'm another wisdom seeker

I would like to have the following data structure:

$Code $Name $Counter $Timestamp
--------------------------------------------------
I have a file where I extract the above elements

example:

06/02/10 17:14:21 17:14:21 xx CODE1 message1 NAME1
06/02/10 17:20:43 17:20:43 xx CODE1 message2 NAME1
06/02/10 17:24:35 17:24:35 xx CODE1 message1 NAME2
06/02/10 17:26:23 17:26:23 xx CODE1 message1 NAME2
06/02/10 17:26:39 17:26:39 xx CODE2 message1 NAME1
06/02/10 17:28:25 17:28:25 xx CODE2 message1 NAME2
06/02/10 17:33:13 17:33:13 xx CODE2 message1 NAME1
06/02/10 18:06:48 18:06:48 xx CODE2 message1 NAME2
06/02/10 18:24:25 18:24:25 xx CODE2 message1 NAME3
06/02/10 18:28:13 18:28:13 xx CODE2 message2 NAME1
06/02/10 18:56:12 18:56:12 xx CODE2 message3 NAME1
06/02/10 18:57:54 18:57:54 xx CODE1 message1 NAME1
06/02/10 18:56:12 18:56:12 xx CODE2 message1 NAME2
06/02/10 18:57:54 18:57:54 xx CODE2 message1 NAME2
06/03/10 00:35:36 00:35:36 xx CODE1 message2 NAME1
06/03/10 00:35:40 00:35:40 xx CODE2 message2 NAME1
06/03/10 00:35:46 00:35:46 xx CODE2 message1 NAME3
06/03/10 00:40:42 00:40:42 xx CODE3 message2 NAME3
06/03/10 00:45:21 00:45:21 xx CODE4 message2 NAME3
06/03/10 00:45:25 00:45:25 xx CODE2 message1 NAME3
06/03/10 00:50:21 00:50:21 xx CODE4 message1 NAME1
06/03/10 00:50:23 00:50:23 xx CODE3 message1 NAME3
06/03/10 00:50:25 00:50:25 xx CODE2 message1 NAME4
06/03/10 00:50:35 00:50:35 xx CODE3 message3 NAME3
06/03/10 00:55:21 00:55:21 xx CODE1 message1 NAME1
06/03/10 00:55:23 00:55:23 xx CODE2 message2 NAME2
06/03/10 00:55:25 00:55:25 xx CODE1 message3 NAME4
06/03/10 01:00:21 01:00:21 xx CODE2 message1 NAME3
06/03/10 01:00:23 01:00:23 xx CODE1 message3 NAME1
06/03/10 01:00:25 01:00:25 xx CODE2 message1 NAME3
06/03/10 01:05:21 01:05:21 xx CODE1 message1 NAME4
06/03/10 01:05:23 01:05:23 xx CODE2 message1 NAME1
06/03/10 01:05:25 01:05:25 xx CODE1 message4 NAME1
06/03/10 01:05:35 01:05:35 xx CODE2 message1 NAME3
06/03/10 01:10:21 01:10:21 xx CODE4 message3 NAME3
06/03/10 01:10:23 01:10:23 xx CODE2 message2 NAME2
06/03/10 01:10:25 01:10:25 xx CODE1 message1 NAME3
06/03/10 01:15:21 01:15:21 xx CODE1 message1 NAME4
06/03/10 01:15:23 01:15:23 xx CODE2 message2 NAME4
06/03/10 01:15:25 01:15:25 xx CODE2 message2 NAME1
06/03/10 01:20:21 01:20:21 xx CODE3 message3 NAME3
06/03/10 01:20:23 01:20:23 xx CODE3 message3 NAME1
06/03/10 01:20:25 01:20:25 xx CODE4 message3 NAME4
06/03/10 01:20:35 01:20:35 xx CODE4 message3 NAME2
06/03/10 01:25:21 01:25:21 xx CODE1 message2 NAME3
06/03/10 01:25:23 01:25:23 xx CODE2 message4 NAME3


List occurrence of code, add to this name, add to name every code-name-pair number of found pairs, list every timestamp found

timestamp = listing of timestamp
The result should look like:


Codex messagex CountCodexMessagex timestamp timestamp (all timestamp listed found form pair of code and message)

exapmle:
CODE1 CountCodexMessagex message1 timestamp, timestamp .. .. .. (all timesta +mp listed found form pair of code and message) CountCodexMessagex message2 timestamp, timestamp .. .. .. (all timesta +mp listed found form pair of code and message) . . . CODE2 CountCodexMessagex message1 timestamp, timestamp .. .. .. (all timesta +mp listed found form pair of code and message) CountCodexMessagex message2 timestamp, timestamp .. .. .. (all timesta +mp listed found form pair of code and message) . . . . . . . . . . actually part of code: if ($Code =~ m/(^EE)(.*)(E$|W$|I$|M$)/) { if ( $entryTestName =~ m/^H_.*/ ) { #print qq(Code: $Code\n); if( defined($hashOfArraysE2E{$Code}{$entryTestName +}) ) { #print qq(juhu\n); my $Count = $hashOfArrays{$Code}{$Name}[0]; #print qq(Count: $Count\n); ## increment counter and add entry time to exi +sting sitcode my $arrName = $Code; my $CodeCount = $hashOfArrays{$Code}[1]; #print qq(sitCodeCountBeforeIncr: $CodeCount\n +); $codeCount++; #print qq(sitCodeCountAfterIncr: $CodeCount\n) +; #print qq(CodeCount: $CodeCount\n); ## allocate new value of count to array $hashOfArrays{$arrName}[1] = $CodeCount; #print qq(hashOfArrays{$arrName}[0]: $hashOfAr +rays{$arrName}[0]\n); push(@{$hashOfArrays{$arrName}},$entryTimeStam +p); #print qq(hashOfArrays{$arrName}: @{$hashOfArr +ays{$arrName}}\n); #print qq(entryTimeStamp: $entryTimeStamp\n); #print qq(existing\n); } else { my $initCount = q(1); push(@{$hashOfArrays{$entrySitCode}{$entryTest +Name}},[$initCount,$entryTimeStamp]); } }
the output is the reference hex:


testsitApplCount: ARRAY(0xc1c814)
testSitApplCount: ARRAY(0xc1ca14)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1c9c4)
testSitApplCount: ARRAY(0xc1cbb4)
testSitApplCount: ARRAY(0xc1c9c4)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)
testSitApplCount: ARRAY(0xc1ce64)


in advice, thanks a lot

Replies are listed 'Best First'.
Re: Hash of hash and array
by kennethk (Abbot) on Jul 16, 2010 at 14:49 UTC
    I'm not sure I can see your question in the above. Part of that issue is that you've wrapped your entire post in <code> tags. Rather than that, you should wrap code, input and output in <code> tags and separate paragraphs with <p> tags. See Writeup Formatting Tips.

    I note that in your output you are printing stringified references rather than the arrays contained in those references. I believe you would get a result closer to what you expect if you change the line

    print qq(testSitApplCount: $testSitApplCount\n);

    to

    print qq(testSitApplCount: @$testSitApplCount\n);

    The @ sigil dereferences the reference to the array it points to. See perlreftut.

    An easier way to visualize a data structure is to use Data::Dumper, a core module. Here is some sample code for that module:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; print Dumper ['foo', 'bar', {a => 1, b => 2}];

    In your case, you would likely feed it:

    print Dumper $hashOfArrays;

    after you finished populating your data structure. See also Basic debugging checklist.

Re: Hash of hash and array
by ahmad (Hermit) on Jul 16, 2010 at 18:12 UTC

    If the data you've shown are the actual data in that file then I would suggest using split instead of regex to get the fields you want.

    If not, then it would have helped a lot provide some real-world data that we can give you some good suggestions.

    Please for your next posts use <p> </p> for your paragraph and <c> </c> for your code part