Run-length encoding: Difference between revisions

Content added Content deleted
Line 1,837: Line 1,837:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>sub encode
<lang perl>
# functional approach (return the encoded or decoded string)
{(my $str = shift) =~ s {(.)(\1*)} {length($&).$1}gse;
sub encode {
(my $str = shift) =~ s {(.)(\1*)} {length($&).$1}gse;
return $str; }
sub decode {
(my $str = shift) =~ s {(\d+)(.)} {$2 x $1}gse;
return $str;}
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]].
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]].