in reply to Arrays and Inline C
If the values in the arrays are unsigned integers, replace SvNV with SvUV, and replace newSVnv with newSVuv.use warnings; use Devel::Peek; use Inline C => <<'EOC'; AV * foo(SV * x, ...) { Inline_Stack_Vars; long array_size, i; AV * ret = newAV(); array_size = SvUV(Inline_Stack_Item(0)); /* check that array_size matches our expectations, based on the total number of arguments supplied */ if(Inline_Stack_Items != (2 * array_size) + 1) croak("Mismatch in number of arguments supplied to foo()"); /* for efficiency, make the array big enough in one fell swoop */ av_extend(ret, array_size - 1); /* multiply corresponding array elements and push the result into ret +*/ for(i = 1; i <= array_size; i++) av_push(ret, newSVnv(SvNV(Inline_Stack_Item(i)) * SvNV(Inline_Stack_Item(i + array_size))) +); return ret; } EOC my @num1 = (2.2, 3.3, 4.4,5.5); my @num2 = (6.6, 7.7, 8.8, 9.9); $arref = foo(scalar(@num1), @num1, @num2); print "@$arref\n"; # prints 14.52 25.41 38.72 54.45
|
|---|