Re: Big problem : find a number of day.



Alextophi wrote:
*-* I must find for a number '$day' the day or the days of the table
%day_array:

my $day = 4;
my %day_array = ("1"  => "0",	# Dimanche
	           "2"  => "1",	# Lundi
	           "4"  => "2",	# Mardi
	           "8"  => "3",	# Mercredi
	           "16" => "4",	# Jeudi
	           "32" => "5",	# Vendredi
	           "64" => "6"	# Samedi
	            );

my $New_day = $day_array{$day};		# result : $New_day = 2


*-* problem: how to make if $day = 5 or 11 or another ?

5  = 4+1	           	# result 2 et 0
11 = 8+2+1	# result 3 et 1 et 0


it is a big problem!

No, it isn't.
Take a look at Perl's binary operators, specifically the shift, and and 1's complement operators:


my $day=11;
my $mask=1;
while ($day) {
    print ' ', $mask if ($day & $mask);
    $day &= ~$mask;
    $mask <<= 1;
}

HTH,

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett

.