Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re (tilly) 1 (duh): Assemble times into ranges

by tilly (Archbishop)
on May 06, 2001 at 04:11 UTC ( [id://78303]=note: print w/replies, xml ) Need Help??


in reply to Assemble times into ranges

The BTree answer was horrible overkill.

It is a great idea if you needed to process the timepoints as they come in. But a naive sorting solution is much simpler and gets better control. After all we have to sort the beginning and end of the intervals Just toss in the timepoints as well. For instance if you want to get closed intervals, you can do it like this:

#!/usr/bin/perl -w use strict; # The original data structure my %interval = qw( 5 50 20 30 70 90 120 130 ); # The times my @times = qw(4 9 30 98 125 500); # Current nesting depth my $depth = 0; # Let us rearrange the interval information # into events consisting of a time/action my @events = map {[$_, 0, sub {++$depth}]} keys %interval; push @events, map {[$_, 2, sub {--$depth}]} values %interval; push @events, map { my $time = $_; [$_, 1, sub {print "Time $time is at depth $depth\n"}] } @times; # Sort it and read out the answer: foreach my $event ( sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @events ) { $event->[2]->(); }
(And, of course, you can make the whole thing much cleaner, this is just proof of concept code.)

Replies are listed 'Best First'.
Re: Re (tilly) 1 (duh): Assemble times into ranges
by Masem (Monsignor) on May 06, 2001 at 06:59 UTC
    More out of curiousity's sake: with this structure, is it possible to determine *which* intervals the events are in (as the original problem seems to ask?). You definitely can determine how many intervals each event falls into via the depth? As per my suggestion, I think you need at least another data structure as you walk the @events array to store which intervals are active.

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

      The depth variable tells you how many events fall in it. If you want to instead know the intervals, you could use a hash there instead. Click for code if you want an example...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found