Hello jweller1, and welcome to the Monastery!

The second method described by trizen above is the best option:

my $ref = $dbFileArray{ 'sci' }; ... push @$ref, $oneRec;

Just to confuse things :-), from Perl version 5.14 onwards you can omit the dereference:

no warnings 'experimental::autoderef'; ... my $ref = $dbFileArray{ 'sci' }; ... push $ref, $oneRec;

— but as that’s still “experimental,” it’s probably more trouble than it’s worth.

I'd like to get a better understanding what @{$dbFileArray{ $part }} actually is (an array? an array reference?)

%dbFileArray is a hash, so $dbFileArray{ $part } is the value associated with the key $part. In this case, $part contains the string 'sci', and the hash entry for that key was previously set via this assignment:

my %dbFileArray = ( 'sci' => \@sciDbRows, ... );

so the value returned by the hash lookup $dbFileArray{ $part } is \@sciDbRows, a reference to an array.

Now, the important syntax to master here is @{...}, which is a dereferencing construct. It takes an array reference, and dereferences it to get the array it refers to. So in this case the expression @{$dbFileArray{ $part }} resolves to the array @sciDbRows. And that’s how pushing to @{$dbFileArray{ $part }} works — it’s really pushing to the array @sciDbRows.

Note that this has nothing to do with the fact that @sciDbRows has been tied to a file. (BTW, in simplifying the example code you forgot to use Tie::File.) The key to understanding what’s going on here is to get a thorough grasp of references in Perl. Begin by studying perlreftut, then look at perllol and perldsc. I found Perl references hard to grasp at first, but with a bit of study and a fair amount of practice their use has become almost second nature. (Well, mostly!)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Looking for some insight on Tie::File by Athanasius
in thread Looking for some insight on Tie::File by jweller1

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.