#!/usr/bin/perl use strict; use warnings; use Statistics::LineFit; use Time::Local; use Data::Dumper; my @x_axis; my @y_axes; sub date_to_epoch { my $date = shift; my ( $y, $m, $d ) = split /-/, $date; return timelocal( '59', '59', '23', $d, $m, $y ); #return timelocal( '0', '0', '0', $d, $m, $y ); } sub max_value { my @array = @_; my $max = $array[0]; for ( my $i = 0; $i <= $#array; $i++ ) { $max = $array[$i] if ( $array[$i] > $max ); } return $max; } my @epochs; while () { next if ( m/^#/ ); chomp; if ( my @line = split /\s+/ ) { my $epoch = date_to_epoch( $line[0] ); # factor down epoch or slope is too shallow. push @x_axis, $epoch; shift @line; for ( my $y = 0; $y <= $#line; $y++ ) { push @{$y_axes[$y]}, $line[$y] ; } } } print Dumper ( \@x_axis ); print Dumper ( \@y_axes ); my $lineFit = Statistics::LineFit->new( 0, 0 ); # TODO change 2nd to 1 $lineFit->setData( \@x_axis, \@{$y_axes[0]} ) or die "Invalid regression data\n"; my ( $intercept, $slope ) = $lineFit->coefficients(); print "Slope(m): $slope Y-intercept(b): $intercept\n"; my %fitline; $fitline{y1} = $intercept; $fitline{x1} = 0; $fitline{y2} = max_value( @{$y_axes[0]} ); $fitline{x2} = ( $fitline{y2} - $fitline{y1} ) / $slope + $fitline{x1}; print Dumper ( \%fitline ); __DATA__ # date notkept hosts 2014-04-01 50 10 2014-04-02 63 11 2014-04-03 120 12 2014-04-04 55 20 2014-04-05 60 22 2014-04-06 63 25 2014-04-07 52 24