in reply to Hash of Arrays or Arrays of arrays? and how proceed?

I'm not quite clear on what you are asking. What parameters do you want to give to the subroutine and what data do you want back? If the question is whether to use HashOfArrays or ArraysOfArrays, a simplicistic answer would be: If you need an ordering or you want to access the data by number, use an array. In most other cases a hash is the right answer.

You seem to want to store an array and a scalar as data. In that case an array as secondary datastructure makes sense, just put the scalar as the first (or last item) into the array. Consequently you can identify it through it being the first (or last) item of the array

You can find lots of examples how a HashOfArrays is accessed in perllol

One other point, the lines where you calculate the successor of a two-character string should be extracted into a subroutine. That would make your script more readable and the algorithm itself better interchangeable and testable

  • Comment on Re: Hash of Arrays or Arrays of arrays? and how proceed?

Replies are listed 'Best First'.
Re^2: Hash of Arrays or Arrays of arrays? and how proceed?
by paride (Initiate) on Jul 29, 2011 at 14:26 UTC

    Thank you very much jethro! I'll search better on the example, even if for me it's difficult convert the fixed examples (@array 1,2,3,red) with the parametrical solution.

    Answering to your first question I would like to pass to the subroutine only the @Databeses array obtained in the Environment defined section and the Table ($file) in which i would like to obtain results. The subroutine (as first instance) have to give me (for every Database of the environment) an array of ordered fields, and a scalar conteining the next available field. I've thinked to an hash but I've not understand how could I pass to the hash the @Database and how I could populate it with the Array that I obtain

    For the second suggestion thanks very much also if I'm a beginner I would like to point directly to the best possible solution.

      I'll try to give you a skeleton of that subroutine:

      sub ExtractDatabaseFields { my $file= shift; my @databases= @_; my %result; foreach my $database (@databases) { my @fields= ... #extract data from database my $nextfield= .... #find next field $result{$database}{next}= $nextfield; $result{$database}{fields}= \@fields; } return \%result; } #------------------- # Accessing my $result= ExtractDatabaseFields($file,@databases); #print next available field of database 27: print $result->{"27"}{next}; #extract the fourth field from database 27: my $field3= $result->{"27"}{fields}[3]; #add another field to database 27: push @{$result->{"27"}{fields}}, "whatever"; #loop over the fields of database $x: foreach $field ( @{$result->{$x}{fields}} ) { .... }

      The resulting data structure would be a HashOfHashOfArrays, not a HashofArrays, but this makes the access to the data much clearer. If this isn't what you wanted you might give a more elaborate example

        Excellent!!! Very Difficult... but Excellent!! THANK YOU VERY MUCH

        now all work ....but I realized I need a more complex solution... :(

        I've transformed $nextfield in an array because of two different DB could have 1 ore more fields less than the other one, and the result is OK! printing Dumper (when @Database is ("5","6"):

        $VAR1 = \'6'; $VAR2 = 'AP', 'AO' ; $VAR3 = 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN' ;

        But to proceed correctly I need a different @fields($VAR3) for every database, and to reach the top of my desired target I would like to add other attributes to the sigle field. For example AA,5(dimension),N(numeric) etc.

        I know it's very complex jethro, when I start with the idea to do this script I didn't expect it will be so complex. But I think this could be an excellent way to learn perl in dept

        I've got a compiling error on

        $result{$database}{next}= $nextfield; $result{$database}{fields}= \@fields;

        Use of uninitialized value in hash element

        but I've declared "my %result;" at the start of the sub

        Did I miss something? Did I need to declare $next and $fields?