in reply to Re: Help me update stubborn perlfaq answers!
in thread Help me update stubborn perlfaq answers!

Fabulous: thanks!

Any chance that someone can explain that bit of code? I've always hated refering someone to code to answer a question. We can take the relevant code to include in the answer, but I'd like a description of it too. :)

--
brian d foy <brian@stonehenge.com>
  • Comment on Re^2: Help me update stubborn perlfaq answers!

Replies are listed 'Best First'.
Re^3: Help me update stubborn perlfaq answers!
by gam3 (Curate) on Apr 23, 2005 at 13:39 UTC
    The main work of the pack1D routine is in the for loop near the bottom. It takes an array of numbers and puts them into a string using a fixed size.
    -------------
    |int|int|int|
    -------------
    
    You can then unpack it with because you know the size of each element in the string.
    #pack work = sv_2mortal(newSVpv("", 0)); for (i=0; i<=n; i++) { work2 = av_fetch( array, i, 0 ); /* Fetch */ if (work2==NULL) nval = 0.0; /* Undefined * else { if (SvROK(*work2)) goto errexit; /* Croak if reference [i.e. not 1D] */ nval = SvNV(*work2); } iscalar = (int) nval; sv_catpvn( work, (char *) &iscalar, sizeof(int)); } #unpack for (i=0; i<m; i++) { av_store( array, i, newSViv( (IV)ivar[i] ) ); }
    -- gam3
    A picture is worth a thousand words, but takes 200K.