Jakob Kofoed wrote:
> Hi all,
>
> I have a file with several columns of numbers, that I want to make sure that
> they are either increasing or are equal to the previously line - if not then
> give a warning.
>
> E.g.
>
> my input file:
>
> 1   2
> 3   3
> 5   4
> 7   5
> 6   6  << Error in column one
> 8   7
>
> I tried to do a foreach loop but could not figure out how to compare e.g.
> column 1/line 1 with column 1/line 2.
>
> If anybody have a suggestion how I get started on this I would greatly
> appreciate it - thanks.

Hi Jakob.

How about this. @prev keeps a copy of the previous line's list
of values. Unfortunately in Perl you can't iterate over two arrays in
parallel, so there's a 'foreach' on the @prev array together with
a count $i which keeps track of the current array index.

HTH,

Rob



  use strict;
  use warnings;

  my @prev;

  while (<DATA>) {

    my @vals = /\d+/g;

    print "[EMAIL PROTECTED]";

    my $i = 0;
    foreach my $p (@prev) {
      next if $vals[$i++] >= $p;
      print "  << Error in column $i";
      last;
    }

    @prev = @vals;
  }

  __DATA__
  1   2
  3   3
  5   4
  7   5
  6   6
  8   7

OUTPUT

  1 2
  3 3
  5 4
  7 5
  6 6  << Error in column 1
  8 7






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to