Re: Checkboxes
[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"; # 100011101100 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 == '01111010' 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 == '01011110' 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
Re: Checkboxes
Jim Hill wrote: > $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: > > > I suspected that there might be solution completely outside my > 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 & $_; >>} > > > I don't follow that. How are the keys in the hash processed such > that they sum to the $flags value? The keys don't necessarily sum to the $flags value. Some of the keys may be present in the $flags value. The ones that are are printed out - determined by the bit-wise and. > Is it possible to get the matching hash values to print out in > the same order in which the hash was initialised? Only if the hash sorts to the same order. You could create an array to re-order the values for you - where $array[0] has the value to lookup up in the hash for the first value, etc. Then the stmt becomes : my @array = (2, 32, 16, 8, 1, 4); for (my $ii = 0; $ii < @array; $ii++) { print "array ordered: $hash{$array[$ii]}\n" if $flags & $array[$ii]; } -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Checkboxes
$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: I suspected that there might be solution completely outside my 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 & $_; > } I don't follow that. How are the keys in the hash processed such that they sum to the $flags value? Is it possible to get the matching hash values to print out in the same order in which the hash was initialised? -- ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Checkboxes
Jim Hill wrote: > Hi all > > The [subscribers] section in the .cfg file of my mailing list > manager gives a flag value for each subscriber ... > > 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? I would just use a hash and for loop. eg: use strict; my $flags = 2; my %hash = (1 => 'Subscriber is Suspended', 2 => 'Receives List Messages', ...); foreach (sort keys %hash) { print "$hash{$_}\n" if $flags & $_; } __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Checkboxes
Code: $bitstring=unpack(b16,$flags); print "Bitstring is $bitstring\n"; # 100011101100 for 1414 HTH :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Jim Hill Sent: Tuesday, January 27, 2004 1:42 PM To: [EMAIL PROTECTED] Subject: Checkboxes Hi all The [subscribers] section in the .cfg file of my mailing list manager gives a flag value for each subscriber ... 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? ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs