#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    67
    Rep Power
    0

    Question compress strings / arrays


    * Situation: I have a large set of indices (in an array or comma-seperated string... take your pick) that needs to be stored in the most compact manner possible. The reasons for doing so I will leave out as to not confuse the problem.

    For example:
    Code:
    var indices = new array(52,68,84,103,104,110);
    indices_enc = asciiEncode(indices);    // run through compression function
    alert(indices_enc);                    // produces "4DTghn"
    (this algorithm/example is so oversimplified- it assumes/requires numbers between 33 and 126 & simply assigns the ASCII equivalent... but I just wanted to make it clear on what I'm trying to do)

    Does anyone know of a Javascript function that compresses an array/csv into a string? I really dont want to have to write a compression algorithm ;.)
  2. #2
  3. Application is what Divides Us
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Dec 2002
    Location
    Titusville, FL
    Posts
    2,179
    Rep Power
    58
    Can you explain what you are trying to do (the Big Picture) ?
    Download [ Fx | Op ] Validate [ Markup | Css ]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    67
    Rep Power
    0
    Originally Posted by jsKid
    Can you explain what you are trying to do (the Big Picture) ?
    [read original post throughly] The reasons for doing so I will leave out as to not confuse the problem. [/read]
  6. #4
  7. Application is what Divides Us
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Dec 2002
    Location
    Titusville, FL
    Posts
    2,179
    Rep Power
    58
    The only way to make this shorter
    Code:
    var indices = new array(52,68,84,103,104,110);
    is to code it this way, which is the literal form of an array..
    Code:
    var indices= [52,68,84,103,104,110];
    but beyond that, without knowing what you're trying to do, I can't say how to "compress" the
    array without you having to rethink/recode your processes in your big picture...

    so, it's obvious what your "simplified" version does.. but..
    how can anybody be of help without some clarification on your side?
    Download [ Fx | Op ] Validate [ Markup | Css ]
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    67
    Rep Power
    0
    maybe thinking of it as "compressing" is confusing you. Think of it as encoding if that'll help. that it is an array makes no difference. take it as a delimited string "52,68,84"... asciiEncode() it... and it turns to "4DT". This gives you 50% "compression". Thats all I'm trying to do (except with an algorithm that gives more like 90%+ compression).

    PM me if you are still confused jskid.
  10. #6
  11. Application is what Divides Us
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Dec 2002
    Location
    Titusville, FL
    Posts
    2,179
    Rep Power
    58
    No... it's not confusing me... but, why do you need to go from numbers to text with the asciiEncode?

    I don't know your situation so it doesn't make much sense to me, that's all.

    Why not just write with straight text?
    Download [ Fx | Op ] Validate [ Markup | Css ]
  12. #7
  13. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    this is why

    Code:
    function asciiEncode(ar) {
      var str='';
      for(var i=0;i<ar.length;i++) str+=String.fromCharCode(ar[i]);
      return str;
    }
    function asciiDecode(str) {
      var str2='';
      for(var i=0;i<str.length;i++) {
        str2+=str.charCodeAt(i)//.toString();
        if(i<str.length-1) str2+=',';
      }
      return str2;
    }
    var indices = new Array(52,68,84,103,104,110);
    var indices_enc = asciiEncode(indices);    // run through compression function
    var indices_dec = asciiDecode(indices_enc);
    alert('52,68,84,103,104,110\n'+indices_enc+'\n'+indices_dec);// produces "4DTghn"
    How could you compress it more than this?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    67
    Rep Power
    0
    thanks for a decent try kravitz (though providing that link guaranteed jsKid's hamster to jump off his wheel). you could get FAR better compression than 50%. Numbers are base 10 (characters 0 to 9)... if you took advantage of a larger character set, you could easily acheive 80% compression at the very least. taking advantage of the fact that it is an ordered set of numbers... I'm sure you can get better than that even. Damn, I should've paid attention in that CSC discrete structures class. where are all the math whizzes?

    Advanced compression technique
  16. #9
  17. Application is what Divides Us
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Dec 2002
    Location
    Titusville, FL
    Posts
    2,179
    Rep Power
    58
    I vaguely remember reading that that thread before...
    (and leave my hamster out of this Mr. Kincaid )
    Download [ Fx | Op ] Validate [ Markup | Css ]
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    Los Angeles, CA
    Posts
    67
    Rep Power
    0
    alot of my posts are more about algorithms, not a particular programming language. I just put it in a particular language forum 'cause I'm hoping someone devised a method in that language already :.\

IMN logo majestic logo threadwatch logo seochat tools logo