in reply to Re^7: Hash of Arrays or Arrays of arrays? and how proceed?
in thread Hash of Arrays or Arrays of arrays? and how proceed?

I enclose the code, it's better, but I don't understand where you suggest to use an hash

sub SubAcquisisciDatiCampiEsistenti { # Chiamata c +on in INPUT $file,@Databases restituisce \%result my $file= shift; my @Databases= @_; my %result; foreach my $Database (@Databases) { my $cmd1 = "adarep db=$Database fdt file=$file "; #print "\n\n $cmd1 \n\n"; my @ElencoDati= (); @ElencoDati = `$cmd1 |grep " 1 I "`; push (@ElencoDati, `$cmd1 |grep "SUPER"`); # my $ClearScr = `clear`; my @array1 = (); my @ElencoCampi = ""; foreach (@ElencoDati) { @array1 = split /\s+/, $_; push (@ElencoCampi, "$array1[3],$array1[5],$array1[7], +$array1[9]"); } shift (@ElencoCampi); @fields = sort (@ElencoCampi); chomp @fields; print "\n Elenco Campi Assegnati sul file $file del DB $Databa +se Ordinati \n"; foreach (@fields) { print "$_\n"; } print "\nL'ultimo campo utilizzato sul file $file del database + $Database e: ", $fields[$#fields],"\n\n\n"; my @Caratteri = split (//,$fields[$#fields]); print "\n"; my $PrimaLettera = ord $Caratteri[0]; print "Il valore numerico nella tabella ASCII della prima lett +era e: $PrimaLettera\n\n"; my $SecondaLettera = ord $Caratteri[1]; print "Il valore numerico nella tabella ASCII della seconda le +ttera e: $SecondaLettera\n\n"; print "\n\n"; if ( $SecondaLettera < 90 ) { $SecondaLettera+=1; push (@nextfield, chr($PrimaLettera).chr($SecondaLettera)) +; # print "\n\nIl prossimo campo disponibile e: $ProssimoCam +po\n\n"; } if ( $SecondaLettera == 90 ) { $PrimaLettera+=1; $SecondaLettera=65; push (@nextfield, chr($PrimaLettera).chr($SecondaLettera)) +; #$nextfield = chr($PrimaLettera).chr($SecondaLettera); # print "\n\nIl prossimo campo disponibile e: $ProssimoCam +po\n\n"; } print Dumper(\$Database,\@nextfield,\@fields); $result{$Database}{next}= \@nextfield; $result{$Database}{fields}= \@fields; } return \%result; }

outside this sub looping to re-print the result of the printing give me only the last fields of the last array two times

Replies are listed 'Best First'.
Re^9: Hash of Arrays or Arrays of arrays? and how proceed?
by jethro (Monsignor) on Aug 05, 2011 at 14:27 UTC

    In your original script you had a line "use strict;". Did you remove that line? It does seem that way, because otherwise the line "@fields = sort (@ElencoCampi);" would have given an error because you are using the global variable @fields instead of a local variable generated with my.

    If you add a line "print \@fields;" to your loop you will see the problem, the reference never changes, it is always the same. But you need a new variable in every iteration of the loop so that they point to different memory locations

    Change that line to "my @fields = sort (@ElencoCampi);" and your problem should go away.

      I haven't removed strict, it work cause I've added at the start of the script "our @fields;" and yes, if I try to print "print \@fields" it doesn't use different addresses, so I've tried to put "my @fields" in the sub and it uses different addresses but outside the sub it still doesn't work. I think I've done some declaration errors...

      It's strange cause if I remove "my @fields" from the sub (using only @fields) it print the last assigned values.....

        What do you mean with "it still doesn't work?". Does it still print the last assigned values or something else?

        One thing I'm very sure, you need the "my" before "@fields" in the sub, otherwise it will never work. That doesn't mean this was the only bug

        What does Dumper tell you directly after returning from the subroutine, i.e. what do you get when you write

        my $xyz= SubAcquisisciDatiCampiEsistenti( ...) #somewhere in your scr +ipt you have a line like this where you call the subroutine print Dumper($xyz); #<----- add this, but use the variable name you +are using

        You should see the complete data in all its details. Does it seem correct? If not, there must be still a bug in the subroutine or how it is called. If all is correct, the bug must be somewhere later

        Now move the Dumper(...) line further and further down your script to check how $xyz looks like in later stages of your script. If you extract data to other variables who later exhibit the problem, check them out with Dumper too. Follow the trail of your data ;-)

        As soon as your data doesn't look correctly anymore you know exactly where the bug is, between the last correct place and this place.

        My guess would be that you forgot to assign the return value of the subroutine to the right place or you are looking at the wrong variables. But you can check yourself, just use Dumper and never take anything for given, test every assumption you have.