#!/usr/bin/perl use warnings; use strict; @ARGV or die "No input file specified"; open my $first, '<', $ARGV[0] or die "Unable to open input file: $!"; open my $second, '<', $ARGV[1] or die "Unable to open input file: $!"; chomp(my @first_lines = <$first>); chomp(my @second_lines = <$second>); close $first; close $second; foreach (@first_lines) { my ($line_letter, $range) = split; my ($range1_low, $range1_high) = split /-/, $range; my $output_line; foreach (@second_lines) { my ($range2_low, $range2_high) = split /-/, $_; my ($current_match, $first_match); foreach my $range2_value ($range2_low..$range2_high) { last if ($range2_value > $range1_high); next if ($range2_value < $range1_low); if (($range2_value >= $range1_low) && ($range2_value <= $range1_high)) { $first_match = $range2_value if (!defined $current_match); $current_match = $range2_value; } } $output_line .= "$first_match-$current_match, " if ((defined $current_match) && ($current_match != $first_match)); $output_line .= "$first_match, " if ((defined $first_match) && ($current_match == $first_match)); } if(defined $output_line) { substr($output_line, length($output_line)-2, 2) = ""; print "$line_letter $output_line\n"; } }