in reply to multiple layers of referencing and dereferencing
I would disagree with that. This looks very obtuse to me! I think that a much better data structure can be constructed. It would be helpful if we backed up and talked about what data you need for each experiment. It sounds like this will wind up being a hash with different values having perhaps reference to list or scalar or simple string or perhaps ref to another hash.
I would go as far as to say that having an array with heterogeneous data types is a bad idea. One of the "power hitter" features of Perl is that it has very cool iteration functions. Most of the time, it is not even necessary to know the "index" of some individual item at all! This is because instead of say a traditional "for loop" ala for (i=1;i<30;i++){work with a[i]}we have foreach my $thing (@a){do something with thing}. You want each thing in an array to be the same so that these array iterators work well.
Anyway for each experiment, %experiment, say, perhaps:
If you want to group experiments all together, then it could be that a ArrayOfHash (AoH), is the right way.$experiment{'title'}='some text'; $experiment{'data'}= [data set]; $experiment{'test condx'}= some ref to another hash table $experiment{'date run'}='some date';
Anyway I think a VAST simplification of your data structure can be made which will result in a VAST improvement in the readability of the code.
So instead of debating how to make your existing syntax work, I suggest backing up to requirements and then proceeding with a data struct based upon that info.
Some small points, you don't need &function() to call function anymore, function(...blah) does it!
Update:
I wrote some simple code as a demo of how a LoH (list of hash) would work...it illustrates a couple of points: 1) nowhere in the code is any kind of [$i] variable! 2)Rather than "going deeper" to make new data sets, this uses the Regex power of Perl to keep things flat - if the key contains 'data' then the key represents a list of stuff.
#!/usr/bin/perl -w use strict; my %experiment1 = ( 'title' => "some title 1", 'data' => [ 1, 2, 3, 4, 5, 6], 'data2' => [10, 11, 12], ); my %experiment2 = ( 'title' => "some title 2", 'data' => [ 100, 101, 103,], 'data2' => [200, 300, 400], 'data3' => [500, 600, 700], ); my @experimentDB = (\%experiment1, \%experiment2); print_all_experiment_titles(\@experimentDB); print "printing dataset(s) for all experiments\n"; print_all_data(\@experimentDB); sub print_all_experiment_titles { my $ref_LoH = shift; foreach my $href (@$ref_LoH) { print "$href->{'title'}\n"; } } sub print_all_data { my $ref_LoH = shift; foreach my $href (@$ref_LoH) { foreach my $key2Lref (sort grep {/data/} keys(%$href)) { print "@{$href->{$key2Lref}}\n"; } } } __END__ The above prints: some title 1 some title 2 printing dataset(s) for all experiments 1 2 3 4 5 6 10 11 12 100 101 103 200 300 400 500 600 700
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: multiple layers of referencing and dereferencing
by Bruce32903 (Scribe) on Jun 23, 2009 at 11:26 UTC | |
by Marshall (Canon) on Jun 26, 2009 at 23:48 UTC |