in reply to Adjacent numbers

Remember the previous line and the previous number. If they are adjacent, print the previous one, and remember to print the next one even if the next pair is not adjacent.
#!/usr/bin/perl use warnings; use strict; my $previous_num = -1; my $previous_line; my $print_next = 0; sub output { my $num = shift; my $adjacent = abs($previous_num - $num) == 1; if ($print_next || $adjacent) { print $previous_line; $print_next = $adjacent; } } while (<>) { my ($num) = /([0-9]+$)/; output($num); $previous_num = $num; $previous_line = $_; } output($previous_num); # Process the last line.

Update: Fixed to catch decreasing numbers, too. Thanks GotToBTru.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Adjacent numbers
by GotToBTru (Prior) on Nov 19, 2015 at 21:56 UTC

    Not included in the sample data, but in the description:

    if ($previous_num + 1 == $num || $previous_num - 1 == $num) {
    Dum Spiro Spero

      It'll take a little more than that.

      It will need to remember the direction in which the numbers are running, otherwise 1,2,1,2,3,2,3,4,3... will be seen as a run.


      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". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        otherwise 1,2,1,2,3,2,3,4,3... will be seen as a run.
        well, they are adjacent, after all. The OP would have to specify, whether e.g 2,3,1 should be treated as group, or not.
        In other words, whether a preceding sort step could distort the desired outcome.