Hi All, I have been trying to write this email for a day or two but the content keeps shifting as my understanding of the problem develops.
I have encountered a bit of code that I am struggling to understand completely. The code is for dealing with database fields that are bigint(20) type. | allowcountry1 | bigint(20) | NO | | -1 | | | allowcountry2 | bigint(20) | NO | | -1 | | | allowcountry3 | bigint(20) | NO | | -1 | | | allowcountry5 | bigint(20) | NO | | -1 | | | allowcountry4 | bigint(20) | NO | | -1 | | | allowcountry6 | bigint(20) | NO | | -1 | | | allowcountry7 | bigint(20) | NO | | -1 | | | allowcountry0 | bigint(20) | NO | | -1 | | The DB table is for a product. The product can only be sold in particularly countries and I think these fields/columns use a bit pattern to represent that. I have tracked down a piece of code that I believe insert the data into each column. I have taken as small a snippet as possible and tried to test it. It produces this: $VAR1 = { 'allowcountry6' => '0xffffffffffffffff', 'allowcountry1' => '0xffffffffffffffff', 'allowcountry5' => '0xffffffffffffffff', 'allowcountry2' => '0xffffffffffffffff', 'allowcountry3' => '0xffffffffffffffff', 'allowcountry4' => '0xffffffffffffffff', 'allowcountry0' => '0xffffffffefffffff', 'allowcountry7' => '0xffffffffffffffff' }; The code that produced this is below and it should run as stood alone. I can understand parts of it but the overall logic is...well, I think beyond me. The routine expect a countryid (156) in this case and a string within a set of tags. The string can be negated so RS.CY1> ANTARCTICA</ means available in all countries EXCEPT ANTARCTICA RS.CY1>* ANTARCTICA</ mean only IN ANTARCTICA. I have put some questions inline and I would really appreciate an help understanding the logic behind this. There are problems elsewhere in the program and I can't really tackling them until I understand how this bit pattern works. Also if anyone has an pointers to reference material that might help with this sort of programming, I'd appreciate it. Mastrering Perl does touch on it but not to this extent. Thanx in advance. Dp. #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hr; my $countryid = 156; my $val = '<RS.CY1> ANTARCTICA</RS.CY1>'; create_bit_mask($val, $countryid, \%hr); print Dumper(\%hr); sub create_bit_mask { my $DEBUG = 1; my $val = shift; my $countryid=shift; my $hr = shift; my $isinclude = 0; # Figure out whether this is an 'allow' or 'deny' entry if( $val =~ /\*/ ){ $val =~ s/\*//; $val =~ s/^\s+(\S.*)/$1/; $isinclude = 1; print STDERR "ProcessCountry ISINC $isinclude\n" if ($DEBUG == 1); } # Have we already got bitmaps? (Authors' comment) if( not exists $hr->{'allowcountry0'} ) { ## Is this how the pattern is negated? my $bmval = $isinclude? ## So it's hex not base 2? '0x0000000000000000' :'0xFFFFFFFFFFFFFFFF'; for(my $i=0;$i<8;$i++) ## Set all the field to there default; either all 0's or F's { $hr->{'allowcountry'.$i} = $bmval; } } # The highest countryid is 243. How can fieldno ever be greater than 3? my $fieldno=int($countryid/64); # 156/64 2.43, fieldno=2 my $str=""; my $currentno=$fieldno*64; # 2 * 64 # Amend the bitmaps. This is necessarily a bit ugly. (Authors comment). for(my $i=0;$i<8;$i++) { my $digits=$hr->{"allowcountry$i"}; ## Changing the bit pattern !!! for(my $j=0;$j<16;$j++) { my $bits=hex( substr($digits,17-$j,1)); if(($countryid - $currentno) < 4 && ($countryid >= $currentno)) { if($isinclude) { $bits |= 1<< ($countryid - $currentno); ### What is this left shift doing? Is that a or complement? } else { $bits &= 15- (1<< ($countryid - $currentno)); ### ditto } } $currentno+=4; substr($digits,17-$j,1,sprintf("%1x",$bits)); } $hr->{"allowcountry$i"}=$digits; print STDERR "allowed ", $digits, "\n"; } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/