Just in time processing on a character stream: Difference between revisions

Added Perl example
(→‎{{header|Perl 6}}: Add a Perl 6 example)
(Added Perl example)
Line 503:
Silence-Dogood.
</pre>
 
=={{header|Perl}}==
{{trans|Perl 6}}
{{libheader|ntheory}}
<lang perl>use strict;
use warnings;
use feature 'say';
use feature 'state';
 
use ntheory qw/fromdigits todigitstring/;
 
my $key = 'perl5';
srand fromdigits($key,36) % 2**63;
 
my @stream;
 
sub stream {
my($i) = shift;
state @chars;
push @chars, chr($_) for 14..127;
$stream[$i] = $chars[rand 1+127-14] unless $stream[$i];
}
 
sub jit_encode {
my($str) = shift;
my $i = 0;
my $last = 0;
my $enc = '';
for my $c (split '', $str) {
my $h;
my $l = '';
++$i until $c eq stream($i);
my $o = $i - $last;
$l = $o % 26;
$h = $o - $l if $o > 26;
$l += 10;
$enc .= ($h ? uc todigitstring($h,36) : '') . lc todigitstring($l,36);
$last = $i;
}
$enc
}
 
sub jit_decode {
my($str) = shift;
my @matches = $str =~ /((.*?) ([a-z]))/gx;
my $dec = '';
my $i = 0;
for my $j (0 .. @matches/3 - 1) {
my $o = ( fromdigits($matches[3*$j+1],36) - 10 // 0) +
( fromdigits($matches[3*$j+2],36) // 0);
$i += $o;
$dec .= $stream[$i];
}
$dec
}
 
my $enc = jit_encode('The slithey toves did gyre and gimble in the wabe');
say my $result = "Encoded\n$enc\n\nDecoded\n" . jit_decode($enc);</lang>
{{out}}
<pre>Encoded
bQh52yf9Ex2Wi4Cv1GcyoAUcBKbke3Mo5Ss2WybQlvQs1GviQc1GnQg5Sv26j1Gm26m3Mp26iQz78lQx2Wel1Gcs52y5Sds3Me26l52au1Gwe1Gl
 
Decoded
The slithey toves did gyre and gimble in the wabe</pre>
 
=={{header|Perl 6}}==
2,392

edits