Hello Jim, Tuesday, January 27, 2004, 7:46:33 PM, you wrote:
JH> $Bill Luebkert in <[EMAIL PROTECTED]>: >> Jim Hill wrote: >> >> > Flags=1414 >> > >> > Is there a module or a perl algorithm for determining which of >> > the 14 checkboxes are enabled from a "Flags=" value? >> >> I would just use a hash and for loop. eg: JH> I suspected that there might be solution completely outside my JH> ken. Thanks $Bill, another brilliant response. >> use strict; >> my $flags = 2; >> my %hash = (1 => 'Subscriber is Suspended', 2 => 'Receives List Messages', ...); >> foreach (sort keys %hash) { >> print "$hash{$_}\n" if $flags & $_; >> } JH> I don't follow that. How are the keys in the hash processed such JH> that they sum to the $flags value? Each value is assigned, in turn, to $_. $_ is then "and"ed with the flags value. JH> Is it possible to get the matching hash values to print out in JH> the same order in which the hash was initialised? A bitwise and (&) will return true if the flag bit is true in a value. Eg: 1414 & 1024 == true # text/plain digests. This might make more sense in binary: 10110000110 # 1414 & 10000000000 # 1024 == 10000000000; # true Bitwise and sets each bit in the return value to 1 if both input bits at the same position are 1. And remember, any non-zero value is a "true" boolean (At least in C, I can only assume the same about perl :P). I also wanted to point out that the binary value for 1414 posted by Mark Messenger is a bit wild. I would suggest against using unpack (at least in that form) to get the binary values of perl integers. Unpack seems to get some of the underlying data structure or at least the binary machine representation in the unpacking. Eg: print unpack(b16,1); 10001100 print unpack(b16,2); 01001100 print unpack(b16,1414); 1000110000101100 print unpack(b16,14); 1000110000101100 print unpack(b32,1414); 10001100001011001000110000101100 It looks like perl's integer data-type is 1 byte per decimal place with the left four bits representing decimal values 0-9 (little endian). The right four bits seem to always be "1100". Heck, those look like the (reversed) ascii values of the characters to me. 00110000 0 00110001 1 00110010 2 00110011 3 00110100 4 00110101 5 00110110 6 00110111 7 00111000 8 00111001 9 Woops, I guess so (just looked it up). Is there some way to force unpack to do this using the integer value? & seems immune, fortunately. -- Best regards, Sam mailto:[EMAIL PROTECTED] _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs