Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: How to sort record using pack function ?

by AnomalousMonk (Archbishop)
on Sep 02, 2010 at 03:28 UTC ( [id://858467]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to sort record using pack function ?
in thread How to sort record using pack function ?

Could you shared how the program knows the record length ?

I'm not sure I understand this question. You have defined the 'record length' to be two bytes (16 bits) in little-endian (or 'VAX') order in the second and third positions in your example string. The 'v' unpack template specifier unpacks just such a value. (The 'x' skips over the first character/byte position.)

  • Comment on Re^3: How to sort record using pack function ?

Replies are listed 'Best First'.
Re^4: How to sort record using pack function ?
by bh_perl (Monk) on Sep 02, 2010 at 03:51 UTC

    You are answer my question. Thank you very much. I am still analyzed how to replace between your query with mine
    My query $length = unpack "C4", substr $data,0,2,''; Your query my ($n) = unpack 'x v', $raw;
      You didn't show what your $data contains, but you seem to be working with the hex representation of the data instead of working on the data represented. Don't convert your data to hex; that just complicates things.

      The example code in my first reply was based on certain assumptions, in particular, that the data you are dealing with is raw binary. My example code may have no bearing on the actual problem you face.

      $length = unpack "C4", substr $data,0,2,'';

      As to your code, my interpretation is:

      • Two characters are taken from the beginning of the string  $data and replaced with the empty string;
      • An attempt is made to unpack four unsigned bytes from the extracted two-character string;
      • Then the first of the four unpacked bytes (only two of which can actualy exist) is stored in  $length and the rest are thrown away.
      This doesn't really seem to make a lot of sense. See code example below.

      >perl -wMstrict -le "my $data = 'ABCDEF'; my $len = unpack 'C4', substr $data, 0, 2, ''; print $len; print qq{'$data'}; " 65 'CDEF'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found