Now, part of the "win" of writing everything that you possibly can in Perl, is that you end up with C interfaces that can be more efficient. You mentioned in chatter that your matrix is rather large. So, let's say that you want to use this C code on the same (or similar) matrix more than once. Then you might want to use something like this:

sub PackMatrix { my $avMatrix= shift(@_); my $nRows= 0 + @$avMatrix; my $nCols= 0 + @{ $avMatrix->[0] }; my $ptrSize= length(pack"p",""); my $ptrLen= $ptrSize*$nRows; my $rowLen= length(pack"f",0)*$nCols; my $dataLen= $rowLen*$nRows; my $packedMatrix; if( pack("p",$packedMatrix) != pack("I",0+\$packedMatrix) ) { die "This platform has strange pointers!"; } length($packedMatrix)= $ptrLen + $dataLen; $packedMatrix= "\0" x $ptrLen; for my $avRow ( @$avMatrix ) { die "Row has ",0+@$avRow," entries instead of $width!" unless $width == 0+@$avRow; $packedMatrix .= pack("f$width",@$avRow); } my $ptr= $ptrLen + \$packedMatrix; for my $i ( 0..$#$avMatrix ) { substr( $packedMatrix, $i*$ptrSize, $ptrSize )= pack "I", $ptr; $ptr += $rowLen; } return \$packedMatrix; }
Then you can pass this single string around (always by reference, never by value) and use it again and again. Sorry, I have to run now so I don't have time to explain this in detail. Feel free to ask questions. (:

        - tye (but my friends call me "Tye")

In reply to (tye)Re4: Passing References to Arrays into Perl Extensions in C by tye
in thread Passing References to Arrays into Perl Extensions in C by Evanovich

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.