Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Compressing a string, but leaving it as a string

by Limbic~Region (Chancellor)
on Mar 12, 2005 at 00:13 UTC ( [id://438845]=note: print w/replies, xml ) Need Help??


in reply to Compressing a string, but leaving it as a string

hacker,
I'm not trying to obfuscate the string,...

I couldn't resist. This is a 8bit <-> 7bit conversion utility so only ASCII 0 - 127 is supported.

#!/usr/bin/perl use strict; use warnings; my $foo = compress( join '', 'a' .. 'z' ); print length $foo, "\n"; print grow( $foo ); sub compress {pack'b*',join'',map{sprintf'%b',ord}split//,pop} sub grow {join'',map{chr oct"0b$_"}grep$_,split/(.{7})/,join'',map{vec +"@_",$_,1}0..length("@_")*8-1}
With a reduction of 12.5%, I doubt you really want to use it though.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Compressing a string, but leaving it as a string
by Anonymous Monk on Feb 17, 2011 at 14:56 UTC

    It seems the compress function cannot handle spaces and digits, etc.

    Should the sprintf not always print 7 digits? In that case, I suggest the following compress function:

    sub compress {pack'b*',join'',map{sprintf'%07b',ord}split//,pop}
      Anonymous Monk,
      Nice catch. As you could probably tell, I posted mostly as a joke and didn't use proper sample data in my testing. I wouldn't use this in any production code.

      Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://438845]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found