#!/usr/bin/perl -w

use 5.034;
no strict 'refs';

# De mi libro sobre la naturaleza de los TAEs, en el anexo en BASIC

unless (-e $ARGV[0]){
    say "USAGE: $0 <datafilename> [n]";
    say "";
    say "Where the datafilename is a plain text file with the same number of integers";
    say "in each row for scaling diferent data characteristics, separated by spaces.";
    say "The first row is discarded, for description of the characteristics or other info.";
    say "";
    say "The optional parameter n is the maximun integer number of the scale. Default = 5";
    say "";
    exit 1;
}

open IN, "<", $ARGV[0] or die $!;

my $scale = $ARGV[1] // 5;    # Escala Likert
die "Escala no numérica" if (0+$scale ne $scale);
die "Escala no entera" if (int($scale) != $scale);
die "¡La escala debe ser entera y positiva!" if ($scale < 1);


my ($count, @line, @matrix, $q, @stats);

my $header = <IN>;
while (<IN>){
    chomp;
    next if (/^#/);
    next if (/^\s*$/);
    @line = split /\s+/, $_;
    $q //= scalar(@line)-1;   # números de datos en la fila
    $count++;
    push @{ $matrix[$count] }, @line; 
}
close IN;
say "$count number of observations readed.";

my $c;
my $flag = 0;
for my $i ( 1 .. $count ){      # líneas de datos
    $c = 0;
    for my $j ( @{ $matrix[$i] } ){  # columnas de datos
	if (0+$j ne $j) {
	    warn "$j not numeric in line ", $i+1; 
	    $flag++;
	}
	if ($j != int($j)){
	    warn "$j not integer in line ", $i+1;
	    $flag++;
	}
	if ($j > $scale) {
	    warn "$j out of scale $scale in line ", $i+1;
	    $flag++;
	}
	$c++;
    }
    if ($c != $q+1) {
	warn "different number of responses ($c instead of ",$q+1, " in line ", $i+1;
	$flag++;
    }
}
die "Errores de lectura $flag" if $flag >0;

for my $i (1 .. $q+1){
    for my $j ($i+1 .. $q+1){   # cruce de valores, [x][x] no tiene sentido y [x][y] = [y][x] 
	next if ($i == $j);
	my $s =0;
	my @sum = ();
	my $total = 0;
	$stats[$j]=0;
	# DUDA!!!
	for my $x (0 .. $scale){
	    for my $y (0 .. $scale){
		$sum[$x][$y] = 0;
	    }
	}
	for my $k (1..$count){
	    for my $x (0..$scale){
		for my $y (0..$scale){
		    if ($matrix[$k][$i-1]==$x && $matrix[$k][$j-1]==$y){
			$sum[$x][$y]++;
			$total++;
		    }
		}
	    }
	}    
	warn "No suma $count" if ($total != $count);
	
	@{ $stats[$i] } = (0) x ($scale+1);
	say "\nData $i / Data $j:\n";
	print "      ";
	for my $y (0 .. $scale){
	    print "     $y  ";
	}
	say "";
	line();
	for my $x (0 .. $scale){
	    print "   $x  |";
	    for my $y (0 .. $scale){
		print " " x (6-length($sum[$x][$y])); 
		print $sum[$x][$y];
		$stats[$i][$y] += $sum[$x][$y];
		$s += $sum[$x][$y];
		print " |";
	    }
	    say "";
	    line();
	}
	print "      ";
	for my $y (0 .. $scale){
	    unless (defined $stats[$i][$y]) { $stats[$i][$y] = 0; }
	    print " " x (7-length(int (100*$stats[$i][$y]/$s))), int(100*$stats[$i][$y]/$s), "%";
	}
	say "";
    }
    say "";
}

exit 3;

sub line {
    print "      ";
    for (0 .. $scale){
	print "+-------";
    }
    print "+\n";
}

__END__
  
I remember my first practices of tabulation and analysis
  of surveys using SPSS pseudocode (XT 5 1/4 floppys).


