Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Comparing packed variables

by GrandFather (Saint)
on Mar 17, 2021 at 23:26 UTC ( [id://11129859]=note: print w/replies, xml ) Need Help??


in reply to Comparing packed variables

Yes:

use strict; use warnings; for my $pair([253, 254], [253, 257]) { my $result = pack("S<", $pair->[0]) cmp pack("S<", $pair->[1]); print "Little: $pair->[0] cmp $pair->[1] = $result\n"; $result = pack("S>", $pair->[0]) cmp pack("S>", $pair->[1]); print "Big: $pair->[0] cmp $pair->[1] = $result\n"; $result = pack("S", $pair->[0]) cmp pack("S", $pair->[1]); print "Native: $pair->[0] cmp $pair->[1] = $result\n"; }

Prints:

Little: 253 cmp 254 = -1 Big: 253 cmp 254 = -1 Native: 253 cmp 254 = -1 Little: 253 cmp 257 = 1 Big: 253 cmp 257 = -1 Native: 253 cmp 257 = 1
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: Comparing packed variables
by Jaap (Curate) on Mar 18, 2021 at 10:33 UTC
    Ah like that.
    Is this a bug in Perl?
    My data is already in native/little endian-ness so i cannot compare it with packed variables?

      No, it's not a bug. String compare works from lowest memory address (first byte) to highest comparing one byte from each string at a time. Little endian puts the least significant byte in lowest memory so the two lowest bytes are compared first which is exactly backwards to what you want.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-24 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found