Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comparison of packed signed integers

by gerleu (Novice)
on Oct 30, 2011 at 11:10 UTC ( #934705=perlquestion: print w/replies, xml ) Need Help??

gerleu has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Please is it possible to check if a signed integer packed in 4 bytes is within a certain range, without unpacking it at first ?

I've tried to pack both the lower and higher boundary signed integers with the 'l' canevas, but using '<' and '>' operators doesn't work...

The reason of my question: I've one huge file containing packed signed integers and I need to compare then to a range of two signed integers, but the unpack operation for each value in the file is too much time-consuming !

Maybe I need to use an other canevas for such purpose ?

Thanks in advance for your miraculous help, Germain

Replies are listed 'Best First'.
Re: comparison of packed signed integers
by BrowserUk (Patriarch) on Oct 30, 2011 at 11:24 UTC

    Updated: To reflect the reality check from johngg below.

    Comparison of packed integer will only work if they are unsigned (or all positive or all negative) and packed in big-endian (Network) order:

    $belo = pack 'N', 10000; $behi = pack 'N', 20000; $ben = pack 'N', 123 +45;; $belo lt $ben && $behi gt $ben and print 'Works';; Works

    Not if they are packed in little-endian (VAX) order:

    $lelo = pack 'V', 10000; $lehi = pack 'V', 20000; $len = pack 'V', 123 +45;; $lelo lt $len && $lehi gt $len and print 'Works';;

    The same thing for packed signed integers (but only works if the numbers are all positive or all negative):

    $bello = pack 'l>', 10000; $belhi = pack 'l>', 20000; $beln = pack 'l> +', 12345;; $bello lt $beln && $belhi gt $beln and print 'Works';; Works $lello = pack 'l<', 10000; $lelhi = pack 'l<', 20000; $leln = pack 'l< +', 12345;; $lello lt $leln && $lelhi gt $leln and print 'Works';;

    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.

      I'm not sure that will work if negative numbers come into it.

      knoppix@Microknoppix:~$ perl -E ' > $pos = pack q{N}, 10000; > say unpack q{B*}, $pos; > $neg = pack q{N}, -10000; > say unpack q{B*}, $neg; > say $pos gt $neg ? q{Good} : q{Bad};' 00000000000000000010011100010000 11111111111111111101100011110000 Bad knoppix@Microknoppix:~$

      You'd probably have to expand the tests to cater for that.

      Cheers,

      JohnGG

        pack template 'N' is for unsigned integers. If you need to cater for signed integers, you need template 'l>'.


        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.

      Hi BrowserUK and thank you for your fast answer :-)

      Here is my code for the file creation.

      open(OUT,'>'.$fil) or die $!; binmode OUT; for $i(1 .. 10000){ $lat=int(rand(20000))+440000; $lon=int(rand(10000))+20000; $typ=chr(31); $cod=pack('l>l>C',$lat,$lon,$typ); print OUT $cod; } close(OUT);
      And here is my code for the read and comparison:

      $lah=pack('l>',$lah); $lab=pack('l>',$lab); $loh=pack('l>',$loh); $lob=pack('l>',$lob); open(IN,"<ch"; @fil=stat(IN); $num=$fil[7]/9; binmode(IN); for(1 .. $num){ read(IN,$lat,4); read(IN,$lon,4); read(IN,$pyt,1); if(($lat lt $lah) && ($lat gt $lab) && ($lon lt $loh) && ($lon gt $lo +b)){ print "OK"; } }

      But it is never OK :-(

        Where are the values of $lah, $lab, $loh, $lob set?


        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://934705]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-06-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (81 votes). Check out past polls.

    Notices?