in reply to help needed on encoding a text file
Before showing you the code, some notes.
Now, I changed the encoding. Now it is:
And now, the code:
Encoder:
#!/usr/bin/perl @lines = <>; binmode STDOUT; $x = 1; foreach $ln (@lines) { @words = split /\s+/, $ln; foreach $word (@words) { $index{$word} = $x++; $count{$word}++; } } $idx=''; foreach $key (keys %index) { $idx.=pack("N(N/a*)",$index{$key},$key); } print STDOUT pack("N",length($idx)),$idx; foreach $ln (@lines) { @words = split /\s+/, $ln; foreach $word (@words) { print STDOUT pack("N",$index{$word}); } }
Decoder:
#!/usr/bin/perl binmode STDIN; undef $/; $i=<>; $is=unpack "N",$i; $ind=substr($i,4,$is);$con=substr($i,4+$is); %index=unpack "(N(N/a))*",$ind; @con=unpack "N*",$con; print join(' ',@index{@con});
Note the use of binmode and the unsetting of $/ (aka input record separator).
--
dakkar - Mobilis in mobile
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: help needed on encoding a text file
by bfdi533 (Friar) on Apr 02, 2003 at 19:55 UTC | |
by dakkar (Hermit) on Apr 03, 2003 at 11:17 UTC | |
by bfdi533 (Friar) on Apr 07, 2003 at 19:38 UTC |