"Roman Y. Asper" wrote:
>
> Hello!
>
> I have a problem with my perl - c embedding!
>
> In c i have a funcion like:
> double sum (double *array, int n)
> {
> double result = 0;
> int i;
>
> for (i=0;i<n;i++)
> result += array[i];
>
> return result;
> }
>
> i want to use it in my .pl file like:
>
> my @array;
> my $length = @array;
> my $sum;
> .
> .
> $sum = (@array, $length);
>
>
> In my .xs file i write:
>
> double
> sum (array, n)
> ???
>
> And there is where i am stuck ...
>
> Can anybody help me with that?
>
> Thanks in advance
>
> Roman Y. Asper
>
> #######
> " So we�ll go no more a-roving
> So late into the night. "
> #######
>
> .
Would something like (in XS XS):
extern int sum(double*,int);
int
sum(sv,num)
SV* sv
int num;
CODE:
{
double *dbls;
AV *av;
int i;
dbls = (double*) malloc (sizeof(double) * num);
av = (AV*)SvRV(sv);
for (i = 0; i < num; i++)
dbls[i] = SvNV(*av_fetch(av,i,0));
RETVAL = sum(dbls,num);
}
OUTPUT:
RETVAL