in reply to pack unpack charcount repetition
ok, so what does it look like?@test = (23,23,4,8,21,90,90,90,90,2,2,2,19,21,19); map { $length = ($test[$_ - 1] == $last)? $length + 1: 1; $run++ unless $test[$_ - 1] == $last; $last = $test[$_ - 1]; $runlengths[$run] = [$test[$_ - 1], $length]; } (1 .. scalar @test);
gives us the set of strings@strings = map { $runlengths[$_][0] . "x" . $runlengths[$_][1] } ( 1 .. $#runlengths);
a final note: no discussion of compression and perl is complete without reference to the Mark Jason-Dominus article on Huffman encoding, which would grace even a royal toilet:sub extract{ my $index = shift; my ($last, $lastindex); foreach (@runlengths[1 .. $#runlengths]){ ($last, $lastindex) = ($$_[0], $lastindex + $$_[1]); return $last if $index <= $lastindex - 1; } return undef; }
|
|---|