Perl: the Markov chain saw PerlMonks

### Re^2: Date Array Convolution

by alanonymous (Sexton)
 on Nov 04, 2011 at 00:30 UTC ( #935801=note: print w/replies, xml ) Need Help??

in reply to Re: Date Array Convolution

**Edited**
1) Your code is awesome, but I understand very little of it.
2) It works almost perfectly, except in the case of overlapping windows, the times are a little off and I can't figure out why.

For example with the input:
```my @listone = (['010000','010110',6],['010200','010210',5],['012350','
+020012',3]);
my @listtwo = (['010005','010015',1],['010207','010211',4],['012355','
+020003',1]);
Do you mind adding some comments so I can figure out how you did that?

Thanks!

Replies are listed 'Best First'.
Re^3: Date Array Convolution
by choroba (Bishop) on Nov 04, 2011 at 09:17 UTC
OK, here is the new code I wrote on the underground on my way to work :-) I used OO this time. The second case is your second example input, if your expected output is different, can you show it?
Update: I read the discussion you had with BrowserUk and tried to accommodate the code appropriately.
Update2: zero-intervals removed from output.
You *Sir* are also a gentleman AND a scholar! I think the output is right, but I'm having trouble understanding exactly what's going on in your code. Like I mentioned to Mr. UK, you guys are awesome at coming up with solutions, but it's hard for newbies to understand it all! I *hate* taking code without understanding it :/
I'll try to explain my code:
The new method just creates an empty object. The object is then populated by the populate method: it just remembers all the start end end points of all the intervals in times subhash. At each interval, it also remembers what value begins to be valid (push) or ceases to (pop).

The most important method is combine. It sorts all the time points and then walks through them, keeping track of all "active" values for the given interval (the active values are stored in @stack). All the values whose intervals start at the given point are inserted into the stack, all values whose intervals end at the given point are popped. The grep { not defined \$value or \$_ != \$value or undef \$value } construct just makes sure repeated values are removed one by one from the stack. Then, if there is no value left, the current output interval is closed and stored in @result. If there was no value before, but now there is, a new interval is started. If the value has changed, the interval is closed and a new one is opened.

Finally, the _day_split function is called on the result. It just examines each interval, and if it spans over several days, it splits it into several intervals. The first and last days are special, because their starting, resp. ending times are derived from the interval.

The _dec and _inc are just helper functions to subtract or add 1 to a time stamp. They are needed to avoid "overlapping" of intervals, i.e. in

```1     7
|-----|  (4)
|--------|  (3)
4        12
the first interval will end at 3, which is _dec(4).

Create A New User
Node Status?
node history
Node Type: note [id://935801]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
As of 2018-03-21 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (263 votes). Check out past polls.

Notices?