Hello, Perl Monks..
I've been struggling to find a suitable solution (or direction) in regards to a parsing task I'm working on. The dataset looks similar to the following (without the dashes - those are simply used here to encapsulate each entry as an example:
Now what I need to do is go through each entry and count the number of <ID>'s assigned to it, adding it to a hash I figure, like so: $myHash{totalForEntry}++, eventually ending up so I can print out a table like: IDS TOTAL 1 23 2 536 3 51 4 353 ..etc I'm not exactly sure how I can break down each section and count the total IDs assigned only within that entry. I had headed in the direction of 'paragraph mode' but what quickly threw me off of there was the empty line within each entry containing a 'deletion-time', which throws off the input separator. Here's some very messy code I had started with, lots of nonsense as I was experimenting here and there:-- rn: uid:<user>, <irrelevant-text> id-info: <URL> | <ID> | <random-string> creation-time: 1366069064 -- rn: uid:<user>, <irrelevant-text> id-info: <URL> | <ID> | <random-string> id-info: <URL> | <ID> | <random-string> id-info: <URL> | <ID> | <random-string> creation-time: 1366069064 -- rn: uid:<user>, <irrelevant-text> id-info: <URL> | <ID> | <random-string> id-info: <URL> | <ID> | <random-string> # random empty line in each entry with 'deletion-time' deletion-time: 1367949064 creation-time: 1366069064 --
#!/usr/bin/perl use DateTime; use strict; use warnings; my $LOGFH = 'testdata'; my $/ = '\n'; my $x=0; open(LOGFH) or die("Couldn't open 'er!"); while(<LOGFH>) { #chomp; print $_, "\n"; $x++; last if $x == 2; } # #print $_, "\n"; # if ($_ =~ m!\|(.*)\|!){ # $id = $1 . "|"; # #print $id; } elsif ($_ =~ m!creationDate:\s+(\d+)!){ ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localt +ime($1); $dt = DateTime->from_epoch( epoch => $1 ); print $dt->year; } #last; }
Monks, do you have any pointers for me in regards to which direction I should be taking this to solve the problem? I'm in no way looking for a handout - I like a challenge and have been thinking this over for the past day, but figured a little community input wouldn't hurt! :) Many thanks, VoidWander
In reply to Parsing multi-line record with varying data by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |