Run-length encoding: Difference between revisions
Content added Content deleted
m (→{{header|C}}: bugfixes ... hopely for all cases... :D) |
((g)awk) |
||
Line 9: | Line 9: | ||
: Input: <code>WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW</code> |
: Input: <code>WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW</code> |
||
: Output: <code>12W1B12W3B24W1B14W</code> |
: Output: <code>12W1B12W3B24W1B14W</code> |
||
=={{header|AWK}}== |
|||
{{works with|gawk}} |
|||
It works with "textual" input. Lines containing numbers are skipped, since they can't be represented in a not ambiguous way in this implementation (e.g. "11AA" would be encoded as "212A", which would be decoded as A repeated 212 times!) |
|||
'''Encoding''' |
|||
<lang awk>BEGIN { |
|||
FS="" |
|||
} |
|||
/^[^0-9]+$/ { |
|||
cp = $1; j = 0 |
|||
for(i=1; i <= NF; i++) { |
|||
if ( $i == cp ) { |
|||
j++; |
|||
} else { |
|||
printf("%d%c", j, cp) |
|||
j = 1 |
|||
} |
|||
cp = $i |
|||
} |
|||
printf("%d%c", j, cp) |
|||
}</lang> |
|||
'''Decoding''' |
|||
<lang awk>BEGIN { |
|||
RS="[0-9]+[^0-9]" |
|||
final = ""; |
|||
} |
|||
{ |
|||
match(RT, /([0-9]+)([^0-9])/, r) |
|||
for(i=0; i < int(r[1]); i++) { |
|||
final = final r[2] |
|||
} |
|||
} |
|||
END { |
|||
print final |
|||
}</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |