Bruce32903 has asked for the wisdom of the Perl Monks concerning the following question:
&fetch_data(\@A); &process_data(\@A); &save_results(\@A);To build this example let's assume I want to process the data associated with several files, experiments, data sets, widgets or whatever. I want to have an array for each data set (B0, B1, ... Bn) that contains the references needed to access that specific data set. The upper level array (A) would contain references to these arrays of references. Thus:
$A[0] = \@B0; # array B0 contains references for data set index_0 $A[1] = \@B1; # array B1 contains references for data set index_1 etc.Now assume the simplified case where each data set has one text array and one hash. References to the data array @Ctn and data hash @Chn will be stored in the data set specific array of references @Bn. Thus:
$B0[0] = \@Ct0; # array Ct0 contains text data for data set index_0 $B0[1] = \%Ch0; # hash Ch0 contains hash data for data set index_0 $B1[0] = \@Ct1; # array Ct1 contains text data for data set index_1 etc.I have made everything described above work properly in the included sample code. I have two main questions:
#!/user/bin/perl use warnings; use strict; my (@A, $A); # single, top level array of references my (@B0, $B0); # mid level array of references for data set index_0 my (@B1, $B1); # mid level array of references for data set index_1 my (@Ct0, $Ct0); # low level array storing text for data set index_0 my (@Ct1, $Ct1); # low level array storing text for data set index_1 my %Ch0; # low level hash storing key-value pairs for data set index_0 my %Ch1; # low level hash storing key-value pairs for data set index_1 print "\nSAMPLE PROGRAM FOR PERLMONKS QUESTION\n"; $A[0] = \@B0; # array B0 contains references for data set index_0 $A[1] = \@B1; # array B1 contains references for data set index_1 $B0[0] = \@Ct0; # array Ct0 contains text data for data set index_0 $B1[0] = \@Ct1; # array Ct1 contains text data for data set index_1 $B0[1] = \%Ch0; # hash Ch0 has key-value pairs for data set index_0 $B1[1] = \%Ch1; # hash Ch1 has key-value pairs for data set index_1 &fetch_data(\@A); &process_data(\@A); &store_data(\@A); sub fetch_data { # populating storage for data set index_0 ${${${$_[0]}[0]}[0]}[0] = 10; ${${${$_[0]}[0]}[0]}[1] = 11; ${${${$_[0]}[0]}[0]}[2] = 12; ${${${$_[0]}[0]}[1]}{"dog"} = 15; ${${${$_[0]}[0]}[1]}{"cat"} = 16; ${${${$_[0]}[0]}[1]}{"bird"} = 17; # populating storage for data set index_1 ${${${$_[0]}[1]}[0]}[0] = 20; ${${${$_[0]}[1]}[0]}[1] = 21; ${${${$_[0]}[1]}[0]}[2] = 22; ${${${$_[0]}[1]}[1]}{"dog"} = 25; ${${${$_[0]}[1]}[1]}{"cat"} = 26; ${${${$_[0]}[1]}[1]}{"bird"} = 27; } sub process_data { # change two items in data set index_1 just to prove I can ${${${$_[0]}[1]}[0]}[2] = 24; ${${${$_[0]}[1]}[1]}{"bird"} = 29; } sub store_data { # printing to screen is good "storage" for a sample program print "TEXT: $_[0]->[0][0][0]\n"; print "TEXT: $_[0]->[0][0][1]\n"; print "TEXT: $_[0]->[0][0][2]\n"; print "\n"; print "HASH: $_[0]->[0][1]{\"dog\"}\n"; print "HASH: $_[0]->[0][1]{\"cat\"}\n"; print "HASH: $_[0]->[0][1]{\"bird\"}\n"; print "\n"; print "TEXT: $_[0]->[1][0][0]\n"; print "TEXT: $_[0]->[1][0][1]\n"; print "TEXT: $_[0]->[1][0][2]\n"; print "\n"; print "HASH: $_[0]->[1][1]{\"dog\"}\n"; print "HASH: $_[0]->[1][1]{\"cat\"}\n"; print "HASH: $_[0]->[1][1]{\"bird\"}\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: multiple layers of referencing and dereferencing
by wfsp (Abbot) on Jun 23, 2009 at 02:45 UTC | |
Re: multiple layers of referencing and dereferencing
by Herkum (Parson) on Jun 23, 2009 at 03:12 UTC | |
by Bruce32903 (Scribe) on Jun 23, 2009 at 11:16 UTC | |
by Herkum (Parson) on Jun 23, 2009 at 15:28 UTC | |
Re: multiple layers of referencing and dereferencing
by Marshall (Canon) on Jun 23, 2009 at 04:10 UTC | |
by Bruce32903 (Scribe) on Jun 23, 2009 at 11:26 UTC | |
by Marshall (Canon) on Jun 26, 2009 at 23:48 UTC | |
Re: multiple layers of referencing and dereferencing
by codeacrobat (Chaplain) on Jun 23, 2009 at 06:12 UTC |