Subject: question about sorting syntax
You do not have a question about sort. You have a question about
pack(). Paring your problem down to get at the heart of your question
will greatly help get you good responses in technical newsgroups like
this one.
On a few pages on the net I've seen this snip of code:
my @sorted=map { substr($_,4) }
sort
map { pack("LA*",tr/eE/eE/,$_) } @words;
In the few pages I've seen, which all have the same snip of code(word
for word in fact), none of the pages explain the parameters passed to
pack().
None? Not even the documentation for pack()?
My question is, and perldoc -pack and Googling the "LA" don't
help, what does "LA*" mean?
Presumably, you meant `perldoc -f pack`, right? I beg to differ with
the assessment that the parameters are not explained in that document:
perldoc -f pack
pack TEMPLATE,LIST
Takes a LIST of values and converts it into a string
using the rules given by the TEMPLATE.
...
The TEMPLATE is a sequence of characters that give
the order and type of values, as follows:
...
A A text (ASCII) string, will be space padded.
...
L An unsigned long value.
(This 'long' is _exactly_ 32 bits)
...
* Each letter may optionally be followed by a
number giving a repeat count. ...
the pack function
will gobble up that many values from the
LIST. A "*" for the repeat count means to
use however many items are left
Okay. So now we know what the format is doing. It's going to look at
the list of values passed to pack(), and create from it a string where
the first four bytes (32 bits) will be the first value converted to a
Long, and the reminder of the bytes will be an ASCII representation of
the remainder of the values.
So now, what are the remaining values passed to pack? Well first we
have the expression 'tr/eE/eE'. You can read the docs yourself in
perldoc perlop, but basically this counts the number of "e" and "E"
characters in $_. So if $_ contained "End of the line", then this
expression would return 3. The final argument is $_ itself.
This tells us that in this example, the first value for pack will be
3, and the remaining value will be "End of the line". The L converts
the 3 to a four-byte representation of the number 3, and the A* simply
treats "End of the line" as a string, which it already is.
So let's see it in action:
$ perl -MData:
umper -le'
$Data:
umper::Useqq = 1;
$str = pack("LA*", 3, "End of the line");
print Dumper($str);
'
$VAR1 = "\0\0\0\3End of the line";
The L converted the number 3 to the four-byte string "\0\0\0\3", and
the A* kept "End of the line" as "End of the line"
This string is then sorted amongst all the other likewise conversions
of the strings in @words, and then the first four bytes - the
"\0\0\0\3" part is returned into @sorted.
I would be very curious to see where this code originated. I'm almost
of the belief that it would have to be an obfuscated Perl contest...
Hope this helps,
Paul Lalli