in reply to finding intermediate range values from two file columns

Well I, and I'm sure a lot of others, do love little code puzzles like this to solve.

I'm confused why in your desired output however the line for "d" includes "45-46" before "51-52". That's the only part of the pattern I can't wrap my brain around. Could you elaborate on that please?

EDIT: The only way that makes sense to me is if your original "file1" input had line "d" as "45-58" instead of "48-58". That or like I said, I'm missing something.

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious
  • Comment on Re: finding intermediate range values from two file columns

Replies are listed 'Best First'.
Re^2: finding intermediate range values from two file columns
by perldigious (Priest) on Aug 09, 2016 at 21:45 UTC

    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"; } }

    I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
    I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious