[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: OT : perl to do this?



Back in the old DOS days I coded some PCX routines in which I learned to lead with the escape character to make the decompress logic easier. So "AAAABCCCDD" would become "!A4B!C3DD". If the data literally had this structure, you could compress further by using a numeral as the escape (and save yourself a byte for each escape). So, "AAAABCCCDD" would become "4AB3C2D" (but you'll have to limit yourself to runs of size 9 or smaller [or whatever your radix is--in an ASCII binary file you could do 64 or more] by emitting two or more sequences if the run is long).

And you're right, since it's simple to detect, you want to code against the pathologic case in which you increase the size of the input. It is arbitrary whether you encode the case where there is no gain.

I also don't know if a slick way to do this in perl but you can certainly write it in perl as easily as in any other language. Unless I'm mistaken (and I'd love to be corrected) working with individual elements of strings is a tiny bit tricky. You can either use unpack to unpack the individual N characters into N variables or you can use substr() to access individual elements.

-Alan

At 08:21 AM 3/3/00 , Bret Hughes wrote:
looks like compression to me and DD < D!2

:)

Bret

Brian wrote:
>
> On Fri, 3 Mar 2000, ::--Koshy Kerteya--~!~ wrote:
>
> > Hi,
> >
> > Suppose I have a string of "AAAABCCCDD"
> > And I wanna use perl to convert this to A!4BC!3DD
> > what should I do ?
> > Is there a ready function (grep???) to achieve this ?
>
> yes perl can do this.
>
> their is no built in function to just magically transform as you have
> above.  It would take a little code.   Why is it:
>
> AAAABCCCDD      A!4BC!3DD
> and not
> AAAABCCCDD      A!4BC!3D!2
>
> I mean if its 1 character, just print it, if its two chars just print it?
> then if its more than 2 do the char!num thing?
>

--- Alan D. Mead / Research Scientist / adm ipat com Institute for Personality and Ability Testing 1801 Woodfield Dr / Savoy IL 61874 USA 217-352-4739 (v) / 217-352-9674 (f)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]