Run-length encoding: Difference between revisions

Content deleted Content added
m →‎decoding: made the DO loop more idiomatic. -- ~~~~
Added COBOL example. Corrected PL/I and R lang tags. Capitalised REXX section headers. Moved Befunge section to correct position.
Line 359: Line 359:
ENDWHILE
ENDWHILE
= o$</lang>
= o$</lang>

=={{header|Befunge}}==
Not the same format as in the example,it puts "n\n" at the beginning so you can pipe the output back in and receive the input.
Pipe the output of the program-it's more reliable.
{{works with|CCBI|2.1}}
<lang Befunge> ~"y"- ~$ v
<temp var for when char changes
format:
first,'n' and a newline. :
a char then a v _"n",v
number then a space continuously 9
example: 1
n > v ,+<
a5 b2
decoded:aaaaabb
the program is ended using decoder
Ctrl-C on linux,or alt-f4
on windows.copy the output >\v encoder
of the program somewhere ^_ $ v
to encode press y : > $11g:, v
to decode pipe file in >1-^ ~ v +1\<
the output of the encoder \ v< $ ^ .\_^
starts with n,this is so ^,:<\&~< _~:,>1>\:v>^
you can pipe it straight in ^ <
~
the spaces seem to be a annoying thing :
thanks to CCBI...if a interpreter dosen't 1
create them it's non-conforming and thus 1
the validity of this program is NOT affected p-
>^
--written by Gamemanj,for Rosettacode</lang>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
Line 603: Line 634:
This example only works if there are no digits in the string to be encoded and then decoded.
This example only works if there are no digits in the string to be encoded and then decoded.


<lang csharp>
<lang csharp> public static void Main(string[] args)
public static void Main(string[] args)
{
{
string input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
string input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
Line 652: Line 682:
}
}
return sb.ToString();
return sb.ToString();
}
}</lang>

</lang>
Somewhat shorter, using Regex.Replace with MatchEvaluator (using C#2 syntax only):
Somewhat shorter, using Regex.Replace with MatchEvaluator (using C#2 syntax only):
<lang csharp>using System;
<lang csharp>using System;
Line 701: Line 731:
(mapcat (fn [[_ n ch]] (repeat (Integer/parseInt n) ch)))
(mapcat (fn [[_ n ch]] (repeat (Integer/parseInt n) ch)))
(apply str)))</lang>
(apply str)))</lang>

=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<lang cobol> >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. run-length-encoding.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION encode
FUNCTION decode
.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 input-str PIC A(100).
01 encoded PIC X(200).
01 decoded PIC X(200).

PROCEDURE DIVISION.
ACCEPT input-str
MOVE encode(FUNCTION TRIM(input-str)) TO encoded
DISPLAY "Encoded: " FUNCTION TRIM(encoded)
DISPLAY "Decoded: " FUNCTION TRIM(decode(encoded))
.
END PROGRAM run-length-encoding.


IDENTIFICATION DIVISION.
FUNCTION-ID. encode.

DATA DIVISION.
LOCAL-STORAGE SECTION.
01 str-len PIC 9(3) COMP.

01 i PIC 9(3) COMP.

01 current-char PIC A.

01 num-chars PIC 9(3) COMP.
01 num-chars-disp PIC Z(3).

01 encoded-pos PIC 9(3) COMP VALUE 1.

LINKAGE SECTION.
01 str PIC X ANY LENGTH.

01 encoded PIC X(200).

PROCEDURE DIVISION USING str RETURNING encoded.
MOVE FUNCTION LENGTH(str) TO str-len
MOVE str (1:1) TO current-char
MOVE 1 TO num-chars
PERFORM VARYING i FROM 2 BY 1 UNTIL i > str-len
IF str (i:1) <> current-char
CALL "add-num-chars" USING encoded, encoded-pos,
CONTENT current-char, num-chars
MOVE str (i:1) TO current-char
MOVE 1 TO num-chars
ELSE
ADD 1 TO num-chars
END-IF
END-PERFORM

CALL "add-num-chars" USING encoded, encoded-pos, CONTENT current-char,
num-chars
.
END FUNCTION encode.

IDENTIFICATION DIVISION.
PROGRAM-ID. add-num-chars.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 num-chars-disp PIC Z(3).

LINKAGE SECTION.
01 str PIC X(200).

01 current-pos PIC 9(3) COMP.

01 char-to-encode PIC X.

01 num-chars PIC 9(3) COMP.

PROCEDURE DIVISION USING str, current-pos, char-to-encode, num-chars.
MOVE num-chars TO num-chars-disp
MOVE FUNCTION TRIM(num-chars-disp) TO str (current-pos:3)
ADD FUNCTION LENGTH(FUNCTION TRIM(num-chars-disp)) TO current-pos
MOVE char-to-encode TO str (current-pos:1)
ADD 1 TO current-pos
.
END PROGRAM add-num-chars.


IDENTIFICATION DIVISION.
FUNCTION-ID. decode.

DATA DIVISION.
LOCAL-STORAGE SECTION.
01 encoded-pos PIC 9(3) COMP VALUE 1.
01 decoded-pos PIC 9(3) COMP VALUE 1.

01 num-of-char PIC 9(3) COMP VALUE 0.

LINKAGE SECTION.
01 encoded PIC X(200).

01 decoded PIC X(100).

PROCEDURE DIVISION USING encoded RETURNING decoded.
PERFORM VARYING encoded-pos FROM 1 BY 1
UNTIL encoded (encoded-pos:2) = SPACES OR encoded-pos > 200
IF encoded (encoded-pos:1) IS NUMERIC
COMPUTE num-of-char = num-of-char * 10
+ FUNCTION NUMVAL(encoded (encoded-pos:1))
ELSE
PERFORM UNTIL num-of-char = 0
MOVE encoded (encoded-pos:1) TO decoded (decoded-pos:1)
ADD 1 TO decoded-pos
SUBTRACT 1 FROM num-of-char
END-PERFORM
END-IF
END-PERFORM
.
END FUNCTION decode.</lang>

{{out}}
<pre>
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
Encoded: 12W1B12W3B24W1B14W
Decoded: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
Line 2,276: Line 2,440:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>declare (c1, c2) character (1);
<lang PL/I>
declare (c1, c2) character (1);
declare run_length fixed binary;
declare run_length fixed binary;
declare input file;
declare input file;
Line 2,314: Line 2,477:
end;
end;
put edit ((c do i = 1 to run_length)) (a);
put edit ((c do i = 1 to run_length)) (a);
end;
end;</lang>
</lang>


=={{header|PowerBASIC}}==
=={{header|PowerBASIC}}==
Line 2,683: Line 2,845:
=={{header|R}}==
=={{header|R}}==
R has a built-in function, rle, for run length encoding. This modification allows input and output in the forms specified above.
R has a built-in function, rle, for run length encoding. This modification allows input and output in the forms specified above.
<lang R>runlengthencoding <- function(x)
<lang rsplus>runlengthencoding <- function(x)
{
{
splitx <- unlist(strsplit(input, ""))
splitx <- unlist(strsplit(input, ""))
Line 2,693: Line 2,855:
runlengthencoding(input)</lang>
runlengthencoding(input)</lang>
Similarly, inverse.rle provides decompression after a run length encoding.
Similarly, inverse.rle provides decompression after a run length encoding.
<lang R>inverserunlengthencoding <- function(x)
<lang rsplus>inverserunlengthencoding <- function(x)
{
{
lengths <- as.numeric(unlist(strsplit(output, "[[:alpha:]]")))
lengths <- as.numeric(unlist(strsplit(output, "[[:alpha:]]")))
Line 2,717: Line 2,879:
=={{header|REXX}}==
=={{header|REXX}}==
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
===encoding===
===Encoding===
<lang rexx>/*REXX program encodes a string by using a run-length scheme. */
<lang rexx>/*REXX program encodes a string by using a run-length scheme. */
parse arg x . /*normally, input would be a file*/
parse arg x . /*normally, input would be a file*/
Line 2,740: Line 2,902:
say 'encoded=' y
say 'encoded=' y
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
'''output''' when using default input:'
'''Output''' when using default input:
<pre style="overflow:scroll">
<pre style="overflow:scroll">
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
Line 2,746: Line 2,908:
</pre>
</pre>


===decoding===
===Decoding===
<lang rexx>/*REXX program decodes a string by using a run-length scheme. */
<lang rexx>/*REXX program decodes a string by using a run-length scheme. */
parse arg x . /*normally, input would be a file*/
parse arg x . /*normally, input would be a file*/
Line 2,770: Line 2,932:
say 'decoded=' y
say 'decoded=' y
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
'''output''' when using the default input:
'''Output''' when using the default input:
<pre style="overflow:scroll">
<pre style="overflow:scroll">
input= 11WB11W2B23WB13W
input= 11WB11W2B23WB13W
Line 3,336: Line 3,498:
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>
</pre>

=={{header|Befunge}}==
Not the same format as in the example,it puts "n\n" at the beginning so you can pipe the output back in and receive the input.
Pipe the output of the program-it's more reliable.
{{works with|CCBI|2.1}}
<lang Befunge> ~"y"- ~$ v
<temp var for when char changes
format:
first,'n' and a newline. :
a char then a v _"n",v
number then a space continuously 9
example: 1
n > v ,+<
a5 b2
decoded:aaaaabb
the program is ended using decoder
Ctrl-C on linux,or alt-f4
on windows.copy the output >\v encoder
of the program somewhere ^_ $ v
to encode press y : > $11g:, v
to decode pipe file in >1-^ ~ v +1\<
the output of the encoder \ v< $ ^ .\_^
starts with n,this is so ^,:<\&~< _~:,>1>\:v>^
you can pipe it straight in ^ <
~
the spaces seem to be a annoying thing :
thanks to CCBI...if a interpreter dosen't 1
create them it's non-conforming and thus 1
the validity of this program is NOT affected p-
>^
--written by Gamemanj,for Rosettacode</lang>