I have a script that gets some data using DBI fetchall_hashref ().
It usually returns a hash, for example:
{ 1 => { id => 1 } }
However, I am only interested in the value of the first item in the hash, which is the maximum value of a particular column. I know that Perl hashes are not ordered, but, fortunately, this particular request always returns exactly 1 or 0 records (since this is a MAX () request).
But the code currently in use is really ugly:
$results->{(keys %{$results})[0]}->{'id'};
Is there a more elegant way to do this? (Without resorting to CPAN modules)
Explanation
I get a hash from the data access layer that we use in the house. Everything returns through fetchall_hashref (). I do not call fetchall_hashref () itself, this is how the data access functions are implemented internally, so they tell me. I am a consumer of this returned data, and this happens in the form of a hash. I'm looking for a more concise way, if one exists, to access query results with a single return value
source share