I have a bit of code that I am working on to encode a text file by replacing each word with a binary code representing it (after building an index of all words in the file) in an effort to save storage (and, of course, to display it later).
I am trying to get the binary code to the file and have tried many different things including printf and pack and have not been able to produce the results that I am after.
Here is my current encode.pl code:
#!/usr/bin/perl
@lines = <>;
#print "Number of lines; $#lines\n";
$x = 0;
foreach $ln (@lines) {
@words = split /\s+/, $ln;
foreach $word (@words) {
$index{$word} = $x++;
$count{$word}++;
}
}
print "-=INDEX=-\n";
foreach $key (keys %index) {
#print "[$key] $index{$key}\n";
#$bkey = pack("C*",$index{$key});
#print $key . chr(0) . $bkey . chr(0);
printf "%s%c%x%c", $key, chr(0), $index{$key}, chr(0);
}
print "\n";
print "-=CONTENTS=-\n";
foreach $ln (@lines) {
@words = split /\s+/, $ln;
foreach $word (@words) {
#print $index{$word} . " ";
#$bword = pack("C*",$index{$word});
#print $bword . chr(0);
printf "%x%c", $bword, chr(0);
}
print "\n";
}
I would be happy for any all all input on this problem.
Ed
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.