#!/usr/bin/perl -w

use 5.034;

use File::Temp qw/ tempfile tempdir /;  # módulo CORE

# $fh = tempfile();
my ($fh, $filename) = tempfile();   # se supone abierto


my (@data, $flag);
if (@ARGV > 1){
    @data = @ARGV;
    $flag=1;
# }elsif (@ARGV > 0){ 
#    if (-e $ARGV[0]){
#    local $/=undef;
#	open IN, '<', $ARGV[0] or die $!;
#	@data = split /\s/, <IN>;              # caso de una sola línea. NO FUNCIONA 
#	close IN;
#	$flag = 3;
#    }
#    die "Not a file.";
}else{
    @data = split /\s+/, <>;
    $flag = 2;
}

die "Error: Not tput utility" if (length(`which tput`)<3);   
my $lines = `tput lines`;
chomp $lines;
$lines--;
$lines--; # version 2
# my $cols = `tput cols`;     # version 2: tiene que tener un número fijo de columas, pero scalar @data
# chomp $cols;
# $cols--;    por observación se puede ver que se puede apurar más

my $cols = 1+scalar @data;

if (scalar(@data) < 3){ #  || @data > $cols){  esto hay que handlearlo
    say "USAGE:";
    say "";
    say "$0 <data1 data2 ... data$cols>";
    say "for printing a simple min-max plot $lines x $cols";
    die "TOO FEW POINTS" if scalar(@data) < 3;
#    warn "TOO MUCH POINTS" if @data > $cols;
}

# XYfile -> no sobra ningún dato
# if (scalar(@data) > $cols){
#    for (my $i = 0; $i <= abs($cols-scalar(@data)) ; $i++){
#	shift @data;  # and one data goes away... across the universe
#    }
#    splice (@data, 0, abs($cols-scalar(@data)));            # WARNING: this must work, but does not! Explained: the reverse things eats all @data
#    shift @data while @data > $cols;
# }

my $max = -9e99;
my $min = 9e99;
my $average = mean(@data);
my $counter = 0;
for my $i (@data){
    $counter++;
#    if (0+$i ne $i && $flag) { die "Not numeric $i at point $counter"; }
    if ($i < $min) { $min = $i; }
    if ($i > $max) { $max = $i; }
#    last if $counter == $cols;
}
die "Error: nothing to plot! (min==max)." if ($min == $max);
my $range = $lines/($max-$min);

my @xy;
for my $y (1..$lines){                     # MIN-MAX AXIS
    $xy[$y][1] = "|";
    $xy[$y][$cols] = "\n";  
    if ($y == $lines){
	$xy[$y][1] = "+";
	for (2..$cols) { $xy[$y][$_] = "-"; }
    }
}

my %h;   # no tienen porqué ser enteros
for my $x (0..$counter-1){
    $h{ $data[$x] } = 1+ int ( ( $max-$data[$x] ) * $range );
    $xy[ $h{ $data[$x] } ][$x] = "0";              # aquí se pasa a línea entera
}

my $avgline = 1 + int( ($max - $average) * $range );


for my $y (1..$lines){
    for my $x (1..$cols){
	if (defined $xy[$y][$x]){
	    print $fh $xy[$y][$x];
	}else{
#	    if ( $y % 20 == 0 && $x % 40 == 0 ){
	    
	    if ($y == int (($lines-1)/2) && $x > 1 && $x % 3 == 0){
		print $fh "·";
	    }elsif ($y == $avgline && $x > 1 && $x % 5 == 0){
		print $fh "*";
	    }else{
		print $fh " ";
	    }
	}
    }
}
close $fh;

system "most $filename";

exit 1;


sub mean {               # mejor reformar XYfile
        my @dat = @_;
        my $sum = 0;
        $sum += $_ for @dat;
        $sum /= scalar @dat;
        return $sum;
}



__END__

Simple min-max plot in xterm

