On Wed, Apr 23, 2008 at 12:17:42AM +0100, Rudolf Lippan wrote:
> On Tue, 22 Apr 2008 11:24:17 +0100, Tim Bunce <[EMAIL PROTECTED]> wrote:
> > Any volunteers, either for both or just the sort function?
>
> Hi Tim,
>
> I would be happy to take a crack at them tomorrow -- actually I have a
> rough draft implementation that I will work on cleaning up and testing.
Great!
Here's a more refined and detailed outline. It's evolved from one I
wrote up for "Sir Woody Hackswell" <[EMAIL PROTECTED]> (who also
expressed off-list an interest in implementing it, though I'd guess
hasn't done much yet).
API:
_concat_hash_sorted( $hash_ref, $kv_separator, $pair_separator,
$value_format, $sort_type )
Then the ParamValues for ShowErrorStatement can be formatted using:
_concat_hash_sorted( $sth->{ParamValues}, "=", ", ", 0, undef )
Here's a rough sketch of the logic broken up into steps that would
roughly match a C implementation:
sub _concat_hash_sorted {
my ( $hash_ref, $kv_separator, $pair_separator, $value_format, $sort_type )
= @_;
# $value_format: false=use neat(), true=dumb quotes
# $sort_type: 0=lexical, 1=numeric, undef=try to guess
$keys = _get_sorted_hash_keys($hash_ref, $sort_type);
my $string = '';
for my $key (@$keys) {
$string .= $pair_separator if length $string > 0;
my $value = $hash_ref->{$key};
if ($value_format) {
$value = (defined $value) ? "'$value'" : 'undef';
}
else {
$value = neat($value,0);
}
$string .= $key . $kv_separator . $value;
}
return $string;
}
sub _get_sorted_hash_keys {
my ($hash_ref, $sort_type) = @_;
if (not defined $sort_type) {
my $first_key = (each %$hash_ref)[0];
$sort_type = looks_like_number($first_key);
}
my @keys = keys %$hash_ref;
@keys = ($sort_type)
? sort sort_numerically @keys
: sort sort_lexicaly @keys;
return [EMAIL PROTECTED];
}
Does that look okay?
(Note that this isn't trying to be general purpose. Like neat() it's a
97.8365% solution that fits the most common needs.)
Thanks!
Tim.
p.s. Some tests with reasonable coverage would be the icing on the cake!