in reply to check for contiguous row values

I did things a bit differently. I would advise having some clear code that talks about the BREAK line rather than using some trick to skip it. Striving for the absolute shortest code is often meaningless. Update: I went ahead and took it the redo loop trick.

I put in way more comments than normal to try to help Op understand how it works.

#!/usr/bin/perl -w use strict; my $last_num=0; my @data =(); while ( my $line=<DATA>) { # Output CASE 1: # A BREAK line just causes the output to be dumped if ($line =~ m/BREAK\s*$/) { output_data(); next; } my ($seq_num, $data) = split(/\s+/,$line); #no need for chomp # Output CASE 2: # when numbers go to far, also a signal to dump output # but we've already read one line too far! if ( ($last_num != 0) and ($seq_num > $last_num+1) ) { output_data(); } # # This is the normal data processing push (@data, $data); $last_num = $seq_num; #print "seq_num = $seq_num data = $data\n"; #debug statement } #Output CASE 4: output_data(); #last record may be here! sub output_data { #Output CASE 3: #won't print anything if data set is less than 2 print "$data[0] $data[-1] ".@data, "\n" if @data>1; @data =(); #erase current data; $last_num =0; #set up for next set } =prints 3DKG000004283 3DKG000004543 4 3DiKG000004569 3DiKG000004879 5 3DiKG000005165 3DiKG000005193 2 3DKG000007633 3DKG000007769 5 3DKG000008189 3DKG000008261 3 =cut __DATA__ 10000058 3DKG000004283 290.48 10000059 3DKG000004315 290.48 10000060 3DKG000004421 1693.9 10000061 3DKG000004543 3118.77 THIS IS A BREAK 10000062 3DiKG000004569 2372.94 10000063 3DiKG000004681 528.87 10000064 3DiKG000004741 187.54 10000065 3DiKG000004773 327.84 10000066 3DiKG000004879 1301.43 10000071 3DiKG000005165 17.94 10000072 3DiKG000005193 13.45 10000074 3DiKG000005261 14.33 10000076 3DiKG000005331 144 THIS IS A BREAK 10000145 3DKG000007633 10.43 10000146 3DKG000007663 10.43 10000147 3DKG000007693 1224.8 10000148 3DKG000007727 1224.8 10000149 3DKG000007769 1359.73 10000162 3DKG000008189 307.62 10000163 3DKG000008231 307.62 10000164 3DKG000008261 14.69

Replies are listed 'Best First'.
Re^2: check for contiguous row values
by gudluck (Novice) on Sep 01, 2010 at 16:17 UTC
    Thank you all for nice suggestions and useful pseudo script and as well working scripts with detailed explanation. I appreciate your prompt replies.