Run-length encoding: Difference between revisions
Content deleted Content added
Line 1,837:
=={{header|Perl}}==
<lang perl>
# functional approach (return the encoded or decoded string)
{(my $str = shift) =~ s {(.)(\1*)} {length($&).$1}gse;▼
sub encode {
sub decode {▼
return $str;}
# procedural approach (modify the argument in place)
▲sub decode
sub encode {
▲ {(my $str = shift) =~ s {(\d+)(.)} {$2 x $1}gse;
$_[0] =~ s {(.)(\1*)} {length($&).$1}gse; }
▲ return $str;}</lang>
sub decode {
$_[0] =~ s {(\d+)(.)} {$2 x $1}gse; }
</lang>
The following modified versions of the previous one, encode/decode a bytes sequence in a way compatible with the functions of the [[Run-length encoding#C|C version]].
|