in reply to Re^2: Any idea for predicting the peak points in the graph by perl
in thread Any idea for predicting the peak points in the graph by perl

Here's a generalization of a snippet I posted earlier (which, BTW, had a my bug, now fixed); it does both maxima and minima:

use strict; my $input_file = 'foobar.txt'; open my $in, $input_file or die "Failed to read $input_file: $!\n"; my @max = do { local $_ = <$in>; chomp; [ split ] }; my @min = ( [ @{ $max[ 0 ] } ] ); my $last = $max[ 0 ][ 1 ]; while ( <$in> ) { chomp; my ( $x, $y ) = split; if ( $y > $last ) { $max[ -1 ] = [ $x, $y ]; } elsif ( $max[ -1 ] ) { $max[ @max ] = undef; } if ( $y < $last ) { $min[ -1 ] = [ $x, $y ]; } elsif ( $min[ -1 ] ) { $min[ @min ] = undef; } $last = $y; } close $in; pop @max unless $max[ -1 ]; pop @min unless $min[ -1 ]; print "Maxima:\n"; print "($_->[ 0 ], $_->[ 1 ])\n" for @max; print "Minima:\n"; print "($_->[ 0 ], $_->[ 1 ])\n" for @min; __END__ Maxima: (4.133, 4580.870000) (4.160, 4544.999000) (4.413, 2473.469000) (4.547, 2663.464000) (4.773, 2588.583000) Minima: (4.147, 4522.753000) (4.307, 1094.119000) (4.467, 2029.188000) (4.680, 814.717000)

the lowliest monk

  • Comment on Re^3: Any idea for predicting the peak points in the graph by perl
  • Download Code