diff options
| author | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-23 15:22:12 +0800 |
|---|---|---|
| committer | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-23 15:22:12 +0800 |
| commit | f299ad624a65d97d1bfac462c8ae859c60ba79a9 (patch) | |
| tree | 158494adcf92a6582d860aa61f9c56b894523e56 | |
| parent | 9370159e464497f67adac2af9a1306a640a413e2 (diff) | |
| download | perlweeklychallenge-club-f299ad624a65d97d1bfac462c8ae859c60ba79a9.tar.gz perlweeklychallenge-club-f299ad624a65d97d1bfac462c8ae859c60ba79a9.tar.bz2 perlweeklychallenge-club-f299ad624a65d97d1bfac462c8ae859c60ba79a9.zip | |
Updated perl5 code of ch-2 to save compressed string to file
| -rw-r--r-- | challenge-022/yet-ebreo/perl5/ch-2.pl | 59 |
1 files changed, 50 insertions, 9 deletions
diff --git a/challenge-022/yet-ebreo/perl5/ch-2.pl b/challenge-022/yet-ebreo/perl5/ch-2.pl index 85ecc2925b..e2af41a6f0 100644 --- a/challenge-022/yet-ebreo/perl5/ch-2.pl +++ b/challenge-022/yet-ebreo/perl5/ch-2.pl @@ -5,7 +5,7 @@ use strict; use bytes; use constant INIT_DICT_SIZE => 255; - +use constant LOG2 => log(2); sub encode { my $string = pop; my $len = length $string; @@ -37,11 +37,42 @@ sub encode { $curr = ""; } push @buff_out, $dictionary{$prev}; - return @buff_out; + + #output as string + my $bin_string = ""; + my $chr_string = ""; + my $dict_index = INIT_DICT_SIZE; + for my $bytes (@buff_out) { + my $width = 0|1+log(++$dict_index)/LOG2; + $bin_string .= sprintf "%0${width}b",$bytes; + + } + + while ($bin_string=~s/.{8}//) { + $chr_string .= chr oct "b$&"; + } + + $chr_string .= chr oct "b".$bin_string.(0 x (8 - length $bin_string)); + return $chr_string; } sub decode { - my @buff_in = @{+pop}; + my $chr_string = pop; + my $bin_string = ""; + my @buff_in = (); + + #decode the string passed + for (0..~-length $chr_string) { + $bin_string .= sprintf "%08b", ord (substr $chr_string, $_,1) + } + + my $dict_index = INIT_DICT_SIZE+1; + my $width = 9; + while ($bin_string=~s/.{$width}//) { + push @buff_in, oct "b$&"; + $width = 0|1+log(++$dict_index)/LOG2; + } + $buff_in[-1]+= oct "b$bin_string"; #Initialize dictionary using numbers as keys #Using array would work too @@ -54,7 +85,6 @@ sub decode { my $c = substr $s, 0, 1; my $ret = $s; - my $count = INIT_DICT_SIZE; #Go through the codes in @buff_in @@ -89,12 +119,23 @@ my $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ei print ("Original String has length ". (length $string)."\n"); print "$string\n\n"; -my @compressed = encode($string); -print ("Compressed String has length ". (@compressed)."\n"); -print (("@compressed "=~s/\d+ /chr $&/gre)."\n\n"); +my $filename = "./compressed.z"; +my $fh; +open( $fh, '>', $filename); +print $fh encode($string); +close $fh; -my $uncompressed = decode(\@compressed); +my $comp_cfile_size = -s $filename; +my $comp_ratio = (length ($string)/ $comp_cfile_size) * 100; + +printf "Compresed file size: ".(-s $filename)." Bytes (Ratio: %0.2f %)\n\n", $comp_ratio; + +open( $fh, '<', $filename); +my $compressed = do { local $/; <$fh> }; +close $fh; + +my $uncompressed = decode($compressed); print ("Uncompressed String has length ". (length $uncompressed)."\n"); print "$uncompressed\n\n"; - +# print "Uncompressed string matches original\n\n" if $uncompressed eq $string; |
