Harry_Sor has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm learning to use perl and I made the following code but it doesn't print the variable "aristas" and "clust", would you help me finding why doesn't the code print those variables?

use strict; use warnings; my $nodos; my $n; my $i; my $j; my $l; my $aristas; my $areales; my $clust; my %red=(); $aristas=0; print"Indica el numero de nodos\n"; $nodos=<STDIN>; #aqui se declara la matriz una vez que se sabe el numero de nodos print"Introduzca las conecciones de los nodos.\n"; for($n=0; $n<$nodos; $n++){ for($i=0; $i<$nodos; $i++){ if($n!=$i){ $red{$n}{$i}=<STDIN>; } } } for($n=0; $n<$nodos; $n++){ $aristas=0; for($i=0; $i<$nodos; $i++){ if($n!=$i){ if($red{$n}{$i}==1){ $aristas++; } } } print"la posicion del nodo en escala vector, numero de aristas + y coeficiente de clustering respectivamente:\n"; print"\n$n"; print"\n$aristas"; if($aristas==1||$aristas==0){ print"0\n"; } else{ $areales=0; for($j=0; $j<$nodos; $j++){ if($n!=$j){ for($l=0; $l<$nodos; $l++){ if($j!=$l&&$l!=$n){ if($red{$j}{$l}==1){ $areales++; } } } } } $areales=$areales/2; $clust=(2*$areales)/($aristas*($aristas-1)); print"$clust"; } }

Replies are listed 'Best First'.
Re: help :)
by GrandFather (Saint) on Dec 02, 2014 at 00:44 UTC

    Without data we can't tell what's happening. I've cleaned up your code somewhat and added a DATA section to it. You should add data to the DATA section that demonstrates the problem. You'll need to tell us what you expect to see compared to what you do see.

    use strict; use warnings; my %red; print "Indica el numero de nodos\n"; chomp (my $nodos = <DATA>); #aqui se declara la matriz una vez que se sabe el numero de nodos print "Introduzca las conecciones de los nodos.\n"; for my $n (0 .. $nodos - 1) { for my $i (0 .. $nodos - 1) { if ($n != $i) { chomp ($red{$n}{$i} = <DATA>); } } } for my $n (0 .. $nodos - 1) { my $aristas = 0; for my $i (0 .. $nodos - 1) { next if $n == $i; ++$aristas if $red{$n}{$i} == 1; } print <<MSG; la posicion del nodo en escala vector, numero de aristas y coeficiente + de clustering respectivamente: $n aristas: '$aristas' MSG if ($aristas == 1 || $aristas == 0) { print "0\n"; next; } my $areales = 0; for my $j (0 .. $nodos - 1) { next if $n == $j; for my $l (0 .. $nodos - 1) { next if $j == $l || $l == $n; $areales++ if $red{$j}{$l} == 1; } } $areales = $areales / 2; my $clust = (2 * $areales) / ($aristas * ($aristas - 1)); print "clust: $clust"; } __DATA__ 2 1 0 0 1

    With the data given the code above prints:

    Indica el numero de nodos Introduzca las conecciones de los nodos. la posicion del nodo en escala vector, numero de aristas y coeficiente + de clustering respectivamente: 0 aristas: '1' 0 la posicion del nodo en escala vector, numero de aristas y coeficiente + de clustering respectivamente: 1 aristas: '0' 0

    but I've no idea if that is what you expect or not. As a general thing, using a DATA section like this can make testing your code much easier.

    Note that I removed all the variable declarations at the start of the program and declared variables where they are first needed. I also used Perl loops instead of C loops and used early exits from loops to avoid needless nesting - that helps make the code easier to understand.

    Perl is the programming world's equivalent of English

      Thank you very much, I tried to convert the following code in perl language, and as an example introduce the number 4 in "Indica el numero de nodos" and introduce the following secuence of numbers in the section "Introduzca las conecciones de los nodos": 1, 1, 1, 1, 0, 0, 1, 0,1, 1, 0, 1.

      I expect to obtain as a result :

      " la posicion del nodo en escala vector, numero de aristas y coeficiente de clustering respectivamente:"

      0

      3

      2/3

      " la posicion del nodo en escala vector, numero de aristas y coeficiente de clustering respectivamente:"

      1

      2

      3

      " la posicion del nodo en escala vector, numero de aristas y coeficiente de clustering respectivamente:"

      2

      2

      3

      " la posicion del nodo en escala vector, numero de aristas y coeficiente de clustering respectivamente:"

      3

      3

      2/3

      #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv){ int nodos, n,i, j, l; float aristas, areales, clust; aristas=0; printf("Indica el numero de nodos\n");//En este bloque es donde se + recopila toda la informacion para hacer el calculo de conectividad y + coeficiente de clustering. scanf("%d",&nodos); int red[nodos][nodos];//aqui se declara la matriz una vez que se s +abe el numero de nodos printf("Introduzca las conecciones de los nodos.\n"); for(n=0; n<nodos; n++){ for(i=0; i<nodos; i++){ if(n!=i){ scanf("%d",&red[n][i]); } } } for(n=0; n<nodos; n++){//Este bloque cuenta el número de coneccion +es de cada nodo (aristas). aristas=0; for(i=0; i<nodos; i++){ if(n!=i){ if(red[n][i]==1){ aristas++; } } } printf(" la posicion del nodo en escala vector, numero de aris +tas y coeficiente de clustering respectivamente:\n"); printf("%d\n",n); printf("%f\n",aristas); if(aristas==1||aristas==0){ printf("0\n"); } else{ areales=0; for(j=0; j<nodos; j++){ if(n!=j){ for(l=0; l<nodos; l++){ if(j!=l&&l!=n){ if(red[j][l]==1){ areales++; } } } } } areales=areales/2; clust=(2*areales)/(aristas*(aristas-1)); printf("%f\n",clust); } } }

        You will be interested to know that the C code and the Perl script are bug for bug compatible - they give the same results.

        Perl is the programming world's equivalent of English
Re: help :)
by NetWallah (Canon) on Dec 02, 2014 at 01:48 UTC
    It is not clear (to me) what you are trying to do, but you may be able to use something like Algorithm::ClusterPoints from CPAN.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams