Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to "zip" a big string to small ?

by pysome (Scribe)
on Oct 28, 2008 at 08:01 UTC ( #719930=perlquestion: print w/replies, xml ) Need Help??

pysome has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks
I have a huge big string to store into DB.
But you know, it take too much space.So i need to zip(or compress) the big string at first,then store it to DB.
e.g.
my $a = 'big big string here ....'; my $b = zip($a);
if the length($a) is far less than length($b) ,then it will be OK.
What's way can be used? Thanks

Replies are listed 'Best First'.
Re: How to "zip" a big string to small ?
by dHarry (Abbot) on Oct 28, 2008 at 08:41 UTC
      Thanks your reply.
      1)The big string ,i mean the string is large enough,about 8-10k
      2)I use the SqlServer ( varchar(max) ) to store the string.
      3)I just compress common string ,eg.:
      my $string = " aaaaaaaaaaaaaaaaaaaaaaaa sdfsdf bbbbbbbbbbb sdfsdfsdf sdfffffffff sdfsdf < tabl> sfsdfffffffffffffffffffffffffffffffffffffffffff ... much and more .... "
        1)The big string ,i mean the string is large enough,about 8-10k

        This doesnít strike me as "big".

        2)I use the SqlServer ( varchar(max) ) to store the string.

        SQLServer should have not trouble handling those strings.

        3)I just compress common string ,eg.:...

        Any of the suggested compression alternatives should work for you. Based on the example you provide I assume that you will obtain excellent compression ratios.

        I would like to draw your attention to one more alternative: RLE which stands for Run Length Encoding. It doesnít give you a great compression ration but itís very simple and therefore fast. I have found a Perl implementation for it which can be found here (I did not tested it though)

Re: How to "zip" a big string to small ?
by ccn (Vicar) on Oct 28, 2008 at 08:06 UTC
Re: How to "zip" a big string to small ?
by zentara (Archbishop) on Oct 28, 2008 at 21:45 UTC
    I don't know how it compares to compression, but you can try a base 128 conversion.
    #!/usr/bin/perl use warnings; use strict; use Math::BaseCnv; #fast my $time = time; print "time -> $time\n"; my $time_128 = cnv( $time, 10, 128 ); print "time_128 -> $time_128\n"; #this will sometimes print hidden newlines exit;

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-03-20 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (59 votes). Check out past polls.

    Notices?