I looked at the 'D' format, and it would work on machines that have 64bit integer hardware, but I need to support some 32 bit only machines.

First, an apology. When I said 'D' template above, I meant 'd'. (I often mix these up as so many of the other lower/upper case pairings, n is the smaller and N is the larger. But for floating point it is 'f' for the smaller and 'd' for the larger. With 'D' being a later addition that is larger still but not supported on 32- bit).

By using 'd' as your template, you can portably (with restrictions) pack and unpack 'integers' upto 2**52 across the 32-bit/64-bit divide:

\perl32\bin\perl -e"$n=2;$n*=2 for 1..52;$n-=20;print pack'd',$n++ for + 1..20" | perl -nE"printf qq[%16.f\n],$_ for unpack'd*', $_" 9007199254740972 9007199254740973 9007199254740974 9007199254740975 9007199254740976 9007199254740977 9007199254740978 9007199254740979 9007199254740980 9007199254740981 9007199254740982 9007199254740983 9007199254740984 9007199254740985 9007199254740986 9007199254740987 9007199254740988 9007199254740989 9007199254740990 9007199254740991

Thus avoiding the need to do custom packing.

Do you see anything wrong with the code?

No. The logic seems fine to me. If you decided to use this in preference to 'd', and will be un/packing long strings of values, then you might want to modify your routines to process variable numbers of values (ie. do the equivalent of 'Q*'), for efficiency.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^7: Will Perl Scripts written on 64 Bit Windows run fine on 32 bit as well? by BrowserUk
in thread Will Perl Scripts written on 64 Bit Windows run fine on 32 bit as well? by Anonymous Monk

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.