Dear monks

I am a perl beginner and i am having trouble understanding the following code snippet

my ($name, $source); $sth->bind_columns(\$name, \$source); my %by_source; push @{$by_source{$source}}, $name while $sth->fetch; $sth->finish; if(scalar keys %by_source) { foreach my $s(sort {$a <=> $b} keys %by_source) { return shift @{$by_source{$s}}; } }
I understand that 2 variables are bound to 2 columns returned from a database query. I believesth->fetch is returning an array for each database row and the first column is assigned to $name and the second column is assigned to $source.But then it looks as though the hash element  $by_source{$source} contains an array ref which is being dereferenced and the value of $name is being pushed onto that array. But that makes no sense as the hash is empty?? Perhaps once i understand this bit I will be able to work out the other lines myself (but an explanation of those lines would be appreciated).

thank you kindly

In reply to help understanding code snippet by Anonymous Monk

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.