in reply to Creating Hash of hash of hash dynamically
You've really got to figure out a way of knowing which columns in this CSV file are 'fields' and which are 'titles'. The following snippet assumes you've already figured that part of the problem out. It assumes that you've dumped the CSV row into @columns, and furthermore, that you know that the number of 'fields' is four.
Given that part of the equation being already resolved, here is one way to build up a hash when the number of 'titiles' (number of levels of nested hashes) is unknown. My strategy builds the hash in reverse, starting at the deepest level and working upward. This is the same technique often used to build linked lists in Perl.
use strict; use warnings; use Data::Dumper; my @columns = qw/ title1 title2 title3 title4 field1 field2 field3 field4 /; my $struct_ref = [ @columns[4..7] ]; # place the 'fields' into an arra +y ref. $#columns = 3; # truncate the original CSV row array, @columns. foreach my $col ( reverse @columns ) { $struct_ref = { $col => $struct_ref }; } print Dumper $struct_ref;
HTH.
Dave
|
|---|