... open( IN, $list2 ) or die "$list2: $!"; while () { next unless ( m:(.+?)#(\d+)$: ); my ( $p, $v ) = ( $1, $2 ); next unless ( exists( $pathname_versions{$p} )); if ( exists( $pathname_versions{$p}{$v} )) { # get here if we have a full match (path and version) } else { # create a sorted list of version numbers from list1: my @list1versions = sort {$a<=>$b} keys %{$pathname_versions{$p}}; if ( $v < $list1versions[0] ) { # list2 version number is lower than _all_ matching list1 entries } elsif ( $v > $list1versions[-1] ) { # list2 version number is higher than _all_ matching list1 entries } } }