[Jim Hill wrote]
[...]
> Flags=1414
> 
> ... which is linked to a set of gui checkboxes - some, but not
> all, of which are mutually exclusive. Checking each option in
> turn and monitoring the .cfg file results in this table ...
> 
>    0   no flags set
>    1   subscriber is suspended
>    2   receives list messages
>    4   posts lists messages
>    8   subscriber is list administrator
>   16   subscriber is moderated
>   32   subscriber is barred
>   64   receives multipart/mixed digests
>  128   receives binaries
>  256   posts binaries
>  512   receives multipart/digest digests
> 1024   receives text/plain digests
> 2048   public membership
> 4096   concealed subscription address
> 8192   force text/plain posts
> 
> Is there a module or a perl algorithm for determining which of
> the 14 checkboxes are enabled from a "Flags=" value?

[Mark Messenger wrote]
[...]
>   $bitstring=unpack(b16,$flags);
>   print "Bitstring is $bitstring\n";  #  1000110000101100  for 1414

Actually, given $flags == 1414, this is unpacking the _string_ "14"
(0x31 0x34).  unpack() expects its 2nd arg to be a string, so Perl
passes the _string_ value of $flags, "1414", to unpack() (only the
first 2 chars of "1414" are used because of the 'b16' template).

To pass unpack() the appropriate byte stream, use pack() first. Such as:
  $bitstring = unpack('b16', pack('S', $flags));
  # $bitstring == '0110000110100000'  LSB -> MSB

As I prefer the MSB (Most Significant Bit) on the left, I would probably
be more likely to use:
  $bitstring = sprintf('%0.16b', 1414);
  # $bitstring == '0000010110000110'  MSB -> LSB

If you would prefer the decimal values of the bits set, consider:
  my @bits = ();
  for (8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1) {
      push(@bits, $_) if $flags & $_;
  }
  # Given $flags == 1414, @bits will contain (1024, 256, 128, 4, 2)

If you want the text corresponding to each checkbox, see $Bill's post.
Hmm...Let me slightly tweak his suggestion and sort the keys in numeric
order and include the bit value in the output - might make it easier to
compare with the checkbox list.

use strict;
my $flags = 1414;  # Just to be consistent with the other examples
my %hash = (1 => 'Subscriber is Suspended', 2 => 'Receives List
Messages', ...);
foreach (sort {$a <=> $b} keys %hash) {
        printf "%4d  %s\n", $_, $hash{$_} if $flags & $_;
}

This prints:
   2  receives list messages
   4  posts lists messages
 128  receives binaries
 256  posts binaries
1024  receives text/plain digests

HTH,
Jonathan D Johnston
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to