in reply to Re: What is the perl equivelant to a 'Set' column type in mysql?
in thread What is the perl equivelant to a 'Set' column type in mysql?
use constant GENRE_ROCK => 0; use constant GENRE_POP => 1; use constant GENRE_JAZZ => 2; use constant GENRE_METAL => 3; use constant GENRE_LAST => 3; sub pack_set { my $bits == 0; foreach my $genre (@_) { $bits |= (1<<$genre); } $bits; } sub unpack_set { my($bits)=@_; my @ret = (); my $i = 0; while ($bits) { if ($bits & 1) { push(@ret,$i); } $i++; $bits >>= 1; } @ret; } my $ella = pack_set(GENRE_POP, GENRE_JAZZ); warn "Packed set is $ella\n"; print "Ella's Genres: "; foreach my $genre (unpack_set($ella)) { $genre == GENRE_ROCK and print "rock "; $genre == GENRE_POP and print "pop "; $genre == GENRE_JAZZ and print "jazz "; $genre == GENRE_METAL and print "metal "; } print "\n";
Storing the numbers in a hash instead of with constants would make it easier to print them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: What is the perl equivelant to a 'Set' column type in mysql?
by tachyon (Chancellor) on May 01, 2004 at 18:06 UTC | |
by sgifford (Prior) on May 01, 2004 at 20:25 UTC | |
by tachyon (Chancellor) on May 01, 2004 at 21:28 UTC | |
|
Re: Re: Re: What is the perl equivelant to a 'Set' column type in mysql?
by nmerriweather (Friar) on May 01, 2004 at 00:34 UTC |