SPUG: pack
Chris Sutton
chris at chriskate.net
Sun Mar 17 15:00:34 CST 2002
My use of pack/unpack
I have some code that generates an html table formatted family tree. The
whole thing works by giving each entry a number which will determine where
they live in the table. There are two parts which determine where they go:
1. which generation they belong to ($g)
2. If they are male or female (1=male, 0=female).
The number assigned to an entry doesn't make any sense unless it's looked at
as a binary representation, which is what I use pack and unpack for.
Lets say we start with me at the bottom of the tree at generation. I don't
get a number because I know where I go, but my father would have a number of
1 which looks like this in binary because he is a male and my mother would
have a number of 0.
Generation 1
1 - father of chris (duh, really boring, but stick with me)
0 - mother of chris
Next we look at the next generation in binary (generation 2)
11 - father of chris's father = 3 decimal
10 - mother of chris's father = 1 decimal
01 - father of chris's mother = 2 decimal
00 - mother of chris's mother = 0 decimal
Next generation (generation 3)
111 - father of chris's fathers father = 7 decimal
110 - mother of chris's fathers father = 3 decimal
101 - father of chris's fathers mother = 5 decimal
100 = 1 decimal
011 = 6 decimal
010 = 2 decimal
001 = 4 decimal
000 = 0 decimal
OK, how do I use pack? In order to get the decimal number, which I use in
another place, I need to manipulate individual bits.
$g = 3; # we are going to work on generation 3
$child = 2; # father of chris's mother (01 in binary)
$t = 1; # means we want to get the number for the father of $child
$b = pack("V",$child);
# takes 2 decimal and makes it into a 32 bit binary number in "V" format and
# which I think was reverse network order (left to right reading or something)
# and this gives us something like 01000000000000000000000...
vec($b,$g-1,1) = $t;
# modifies the binary pack and sets bit 2 to 1 so our binary pack $b looks
like 011000000000000000000000...
$n = unpack("V",$b); # converts that binary back into decimal 6
This is probably a trival use of pack and unpack but it took me a while to
figure out that I could even use pack and unpack to solve my problem which I
think is what Tim was talking about.
For those interested here is a link to what the tree actually looks like.
http://www.chriskate.net/tree.pl?pid=1
The only links that really work to navigate are the [+]'s which move you
around the tree. I'm working on the rest at the moment.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list