On Sun, 19 Jun 2005, Will Lowe wrote:

> I've got some XS code that needs to return a hash reference.  The hash
> keys are integers, and the values will all be "undef".  The relevant
> chunk of code looks something like this:
> 
> RETVAL = (HV*) sv_2mortal( (SV*) newHV );
> /* call some code in an external library */
> num_results = find_results( data, &results );
> if( num_results ) {
>    SV* value;
>    for( x = 0; x < num_results; x++ ) {
>             value = &PL_sv_undef;
>             fprintf(stderr, "XS: returning %u => undef\n", results[x]);
>        }
>        hv_store_ent(RETVAL, sv_2mortal(newSViv(results[x])), value, 0);
>     }
> }
> OUTPUT:
>       RETVAL
> 
> ... but Data::Dumper is confused. Here's the output of the fprintf()s
> above, and what Data::Dumper thinks of the returned hashref, when
> results = [3,4,5]:
> 
> XS: returning 3 => undef
> XS: returning 4 => undef
> XS: returning 5 => undef
> $VAR1 = {
>           '3' => undef,
>           '4' => ${\$VAR1->{'3'}},
>           '5' => ${\$VAR1->{'3'}}
>         };
> 

What happens if you try to return 'n' distinct values? 


Reply via email to