Re: Sort hash keys in C...

Rudolf Lippan <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.sybase.devel
Message-ID <[email protected]>

On Wed, 23 Apr 2008 09:34:13 +0100, Tim Bunce <[email protected]> wrote:

> Here's a more refined and detailed outline. It's evolved from one I
 
aka more involved ;)

> 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 \@keys;
> }
> 
> Does that look okay?

Looks fine. I did draft of the above (without the neatsvpv for right now as
I need to dig into that some more), and I have couple of questions:

1. You no longer want _concat_hash_keys() ?
2. How closely are you expecting the C API to match the perl api?  It looks
like the C stuff would have to do less work if it were to pass some of the
string lengths around.
3. Do we worry about Magic here?

> 
> p.s. Some tests with reasonable coverage would be the icing on the cake!

Of course!


-r
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.