in reply to hashing intervals
#!/usr/bin/perl use warnings; use strict; use feature qw(say); { package Triplets; sub new { return bless [], shift; } sub add { my $self = shift; push @$self, \@_; } sub sort { my $self = shift; @$self = sort { $a->[0] <=> $b->[0] } @$self; } sub retrieve { my $self = shift; my $value = shift; my $idx = 0; $idx++ while $idx <= $#$self and $self->[$idx][0] <= $value; return $self->[$idx-1][-1] if $idx and $self->[$idx-1][1] >= $ +value; return; } } my $triplets = 'Triplets'->new; while (<DATA>) { my ($int, $value) = split; my ($from, $to) = split /-/, $int, 3; $triplets->add($from, $to, $value); } $triplets->sort; say "$_: ", $triplets->retrieve($_) // 'Undefined' for 1 .. 135; __DATA__ 23-45 12 11-17 2 46-134 23 fixed here!!! 10-10 65 1-1 45 19-20 10
|
|---|