Run-length encoding: Difference between revisions
Content added Content deleted
Line 1,837: | Line 1,837: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang perl> |
<lang perl> |
||
# functional approach (return the encoded or decoded string) |
|||
⚫ | |||
sub encode { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return $str;} |
return $str;} |
||
# procedural approach (modify the argument in place) |
|||
⚫ | |||
sub encode { |
|||
⚫ | |||
$_[0] =~ s {(.)(\1*)} {length($&).$1}gse; } |
|||
⚫ | |||
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]]. |