#!/usr/bin/perl -w

use strict;

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

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

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

my $c;
my $flag = 0;
for my $i ( 1 .. $count ){
    $c = 0;
    for my $j ( @{ $matrix[$i] } ){
	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){
	next if ($i == $j);
	my @sum = ();
	my $total = 0;
	# DUDA!!!
	for my $x (0 .. $scale){
	    for my $y (0 .. $scale){
		$sum[$x][$y] = 0;
	    }
	}
	for my $k (1..$count){
#	    for my $z (1..$count){
#		if ($z != $k){
		    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);
	
	print "\nData $i / Data $j:\n\n";
	print "      ";
	for my $y (0 .. $scale){
	    print "     $y  ";
	}
	print "\n";
	line();
	for my $x (0 .. $scale){
	    print "   $x  |";
	    for my $y (0 .. $scale){
		print " " x (6-length($sum[$x][$y])); 
		print $sum[$x][$y];
		print " |";
	    }
	    print "\n";
	    line();
	}
    }
    print "\n";
}

exit 2;

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).


