Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am a perl beginner and i am having trouble understanding the following code snippet
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 kindlymy ($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}}; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: help understanding code snippet
by Anonymous Monk on Apr 28, 2011 at 00:16 UTC | |
|
Re: help understanding code snippet
by anonymized user 468275 (Curate) on Apr 28, 2011 at 14:18 UTC | |
by Eliya (Vicar) on Apr 28, 2011 at 14:59 UTC |