Assume for a moment we have a large directory full of files. These files are roughly 2000 lines long. We want to gather data about the contents of these files. It is not necessary to associate data with filenames, just data with line numbers. So, we construct a hash with line numbers as keys. It looks like this:
my %statistics = ( 1 => [ "foo", "bar" ], 2 => [ "baz", "bletch" ], 3 => [ "quux" ], );
But, how do you decide when to add an arrayref or a scalar? I was thinking there should be a add-in-array-context operator for this, and I even vaguely recall that this was RFC'd, using  :=. Now, granted, I hate Pascal, so I'm not keen on using that operator. I also recall that one of Larry's rules in Apocalypse 1 was "Larry Gets The Colon." So the add-in-array-context idea was shot down (i think). So instead of having my nifty operator, I have this less-than-slick code:
# if we havent hit this line yet, add an array # reference to %statistics at this line number. if (not defined $statistics{$line_number} ) { $statistics{$line_number} = [ $thisline ]; } # if we have hit this line, then we have an # array reference already and all we have to do # is push it. if (defined $statistics{$line_number} ) { push @{ $statistics{$line_number} }, $thisline; }
Which seems to be a) semi-fragile and b) inelegant. Note: sometimes we will hit a piece of the file that says "we dont need to know any more about this." So we stop parsing at that point. We also dont know how long (or short) these files are going to be. So we cant very well just populate the hash with array refs (which would be an ugly kludgeful way to do this, imho). Is there a smarter way to do this?

brother dep

--
Laziness, Impatience, Hubris, and Generosity.


In reply to Larry Gets The Colon. (selective addition of an arrayref to a hash) (code) by deprecated

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.