in reply to Re: finding intermediate range values from two file columns
in thread finding intermediate range values from two file columns
Rats! You beat me to the punch (post?) again choroba and haukex. Your guys code-foo is strong.
UPDATE: Ah, but mine does keep his specified formatting, so I'm claiming the bonus points :-).
Well, here is my solution anyway. I'm a lot more long winded with my code than the other monks here...
#!/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 <= $r +ange1_high)) { $first_match = $range2_value if (!defined $current_mat +ch); $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"; } }
|
|---|