Result:#!/usr/bin/perl -w use strict; use Data::Dumper 'Dumper'; { package Range; sub new{ my $class = shift; return bless {map { $_=> shift} qw[NAME START FIN]}, $class; } sub contains{ my ($self, $other) = @_; return $self->{START} <= $other->{START} && $self->{FIN} >= $other->{FIN} ; } sub add_subrange{ my ($self, $other) = @_; push @{$self->{SUBRANGES}}, $other; } 1 } #--- Main code --- my @ranges = ( new Range(qw[A 11 22]), new Range(qw[B 22 45]), new Range(qw[C 22 33]), new Range(qw[D 25 28]), new Range(qw[E 47 49]), ); for my $r1 (@ranges){ for my $r2(@ranges){ next if $r1 == $r2; $r1->add_subrange($r2) if $r1->contains($r2); } print Dumper \$r1; }
$VAR1 = \bless( { 'NAME' => 'A', 'START' => 11, 'FIN' => 22 }, 'Range' ); $VAR1 = \bless( { 'NAME' => 'B', 'START' => 22, 'SUBRANGES' => [ bless( { 'NAME' => 'C', 'START' => 22, 'FIN' => 33 }, 'Range' ), bless( { 'NAME' => 'D', 'START' => 25, 'FIN' => 28 }, 'Range' ) ], 'FIN' => 45 }, 'Range' ); $VAR1 = \bless( { 'NAME' => 'C', 'START' => 22, 'SUBRANGES' => [ bless( { 'NAME' => 'D', 'START' => 25, 'FIN' => 28 }, 'Range' ) ], 'FIN' => 33 }, 'Range' ); $VAR1 = \bless( { 'NAME' => 'D', 'START' => 25, 'FIN' => 28 }, 'Range' ); $VAR1 = \bless( { 'NAME' => 'E', 'START' => 47, 'FIN' => 49 }, 'Range' );
"As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom
In reply to Re: sorting and grouping by
by NetWallah
in thread sorting and grouping by
by jperlq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |