Over the years there have been numerous times when I passed arrays by reference to subroutines. I have often been a little challenged by it. Now I want to start another program and I'd like to seek a little wisdom before I get started.

I want to be able to easily pass data to various subroutines by using references. To build an example for this posting I'll use an array "A" to hold the references to the data. Thus, my calling code would look like this:
&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:

1) Is my syntax and method of working with the data valid? I would hate to be using references to "spray data" all over the computer and sooner or later corrupt something or crash the system.

2) The $_[0]->[0][0][0] syntax for recovering data is not too bad. But the ${${${$_[0]}[1]}[0]}[2] = syntax for storing data is rather messy. I tried to store using the same syntax as recovering but it did not work. Is there cleaner syntax for storing data?

My sample code is in the READMORE area below.
#!/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"; }

In reply to multiple layers of referencing and dereferencing by Bruce32903

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.