Re: Big problem : find a number of day.
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 04 Nov 2005 10:40:53 +0100
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
.
- References:
- Big problem : find a number of day.
- From: Alextophi
- Big problem : find a number of day.
- Prev by Date: Big problem : find a number of day.
- Next by Date: Re: just because is a tiger (was: Re: s///x)
- Previous by thread: Big problem : find a number of day.
- Next by thread: Re: Big problem : find a number of day.
- Index(es):