There's a few completely correct, but strange answers given here to what I thought would be a simple question.
The short answer is to use the
vec function, which can store numbers 0-255 (i.e. unsigned 8-bit numbers) in a single byte. You can store 16-bit, 32-bit or any other length that comes to mind as well.
A simple way is to use a scalar as an array, and just direct-assign to it:
my $n;
my $foo;
my @offsets = (0, 1, 3950122);
foreach (@offsets)
{
vec($foo,$_,8) = $n++;
}
foreach (@offsets)
{
print "$_ = ",vec($foo,$_,8),"\n";
}
Remember, though, that like an array, if you use bits that are really far out in left field (i.e. millions), you are going to use a ton of memory.
This is just like what
merlyn said but with a bit of explanation.