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

Oh yes, you will be an expert on references and complex data structures after this ;-)

One advice: You could store your attributes (i.e. AA, 5 'N' etc.) in strings (concatenated by ':' for example) or in arrays (i.e. index 0 would be 'AA', index 1 the dimension...). But usually the most maintainable way to do this is with a hash.

#add values $fields{$database}[$fieldno]{dimension}=5; $fields{$database}[$fieldno]{type}='N'; # check if an attribute 'something' exists if (exists $fields{$database}[$fieldno]{something}) {

Replies are listed 'Best First'.
Re^6: Hash of Arrays or Arrays of arrays? and how proceed?
by paride (Initiate) on Aug 04, 2011 at 07:56 UTC

    Thanks, I've choosen the string solution (AA,5,N...) and it work perfectly, but if I need to have a different @fields stored for each database? it works in sub but outside when I do the loop for each $Database to re-print dumper using

    Dumper(\$Database,\@nextfield,\@fields);

    It print only the fields of the "last" $Database acquired... did you have an idea about it?

      ...but if I need to have a different @fields stored for each database?

      Well, I told you the third solution with hashes is the best, because there undefined fields are not a problem at all. With strings you always have to set all fields. You just define the empty string (or a value that is not a valid value) for any field as an empty field. So if for example the dimension is undefined for a database you would have (AA,,N...). But using the empty string is only possible if it is not a valid value anywhere. If that is not the case you would have to use some ugly construct where '@@@' or something means undefined

      It print only the fields of the "last" $Database acquired

      '$Database' is not '$database'. upper/lower case is important.

        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