in reply to deleting elements in nested hash structure

Something like this?

#! perl -slw use strict; use Data::Dump qw[ pp ]; my %tss = map { my $s = int( rand 1000 ); $_ => { start => $s, stop => $s + int( rand 100 ) } } 1 .. 100; #pp \%tss; my @ids = sort{ $a <=> $b } keys %tss; my @toDelete; for my $first ( 0 .. $#ids ) { for my $second ( $first+1 .. $#ids ) { ## updated per NetWallah's suggestion below push( @toDelete, $ids[ $first ] ), last unless $tss{ $ids[ $first ] }{ start } > $tss{ $ids[ $seco +nd ] }{ stop } or $tss{ $ids[ $first ] }{ stop } < $tss{ $ids[ $seco +nd ] }{ start }; } } #pp \@toDelete; delete @tss{ @toDelete }; pp \%tss; __END__ C:\test>junk { 74 => { start => 982, stop => 1011 }, 80 => { start => 572, stop => 577 }, 87 => { start => 286, stop => 319 }, 90 => { start => 600, stop => 674 }, 91 => { start => 252, stop => 266 }, 92 => { start => 1, stop => 7 }, 94 => { start => 210, stop => 221 }, 95 => { start => 553, stop => 554 }, 96 => { start => 113, stop => 158 }, 97 => { start => 752, stop => 820 }, 98 => { start => 543, stop => 550 }, 99 => { start => 436, stop => 456 }, 100 => { start => 696, stop => 725 }, }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: deleting elements in nested hash structure
by NetWallah (Canon) on Jun 02, 2012 at 17:31 UTC
    um .. shouldn't that be
    if ( $tss{ $ids[ $first ] }{ start } > $tss{ $ids[ $second + ] }{ stop } or $tss{ $ids[ $first ] }{ stop } < $tss{ $ids[ $seco +nd ] }{ start } ){ # No Overlap }else{ push @toDelete, $ids[ $first ]; # <<<< Note: changed $ +first to $ids[first] last; # <<<< Added 'last' to avoid deleting $ +first multiple times }

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      last; # <<<< Added 'last' to avoid deleting $first multiple times

      It doesn't change the result, but yes, it is a more optimal implementation. I wouldn't code it that way though.

      Whilst if( noOverlap ) { do nothing } else { push @todelete }

      is functionally equivalent to push @toDelete unless noOverlap;

      I find the latter easier on both the eye and the brain.

      Hence, I#ll modify the above this way:

      push( @toDelete, $first ), last unless $tss{ $ids[ $first ] }{ start } > $tss{ $ids[ $seco +nd ] }{ stop } or $tss{ $ids[ $first ] }{ stop } < $tss{ $ids[ $seco +nd ] }{ start };

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        You are still missing the first correction - instead of using $first directly, you need to use $ids[$first].

        Either

        push( @toDelete, $ids[$first]), last
        or
        delete @tss{ @ids[@toDelete] };

                     I hope life isn't a big joke, because I don't get it.
                           -SNL