Category Archives: string manipulation

N to the power of N (with strings)

Actionscript:
  1. var input:String =  "a, b, c";
  2.  
  3. var words:Array = input.split(", ");
  4. var max:String = "";
  5. var maxD:String =  (words.length - 1).toString();
  6. for (var i:int = 0; i<words.length; i++){
  7.     max += maxD;
  8. }
  9. var maxInt:int = parseInt(max,words.length);
  10.  
  11. for(i = 0; i<=maxInt; i++){
  12.      var indices:String = i.toString(words.length);
  13.      var r:String = "";
  14.      var k:int=0;
  15.      for (var j:int = 0; j<indices.length; j++){
  16.          r += words[parseInt(indices.charAt(j))] +" ";
  17.          k++;
  18.      }
  19.      while(k <words.length) {
  20.         r = words[0] +" "+ r;
  21.         k++;
  22.      }
  23.     trace(r);
  24. }
  25. trace(i, " variations");

Like many things on this site, I coded this rather quickly and it can probably be cleaned up...

Setting the input variable of the above snippet to "a, b" will output:

a a
a b
b a
b b
4 variations

Setting the input to "a, b, c" will output:

a a a
a a b
a a c
a b a
a b b
a b c
a c a
a c b
a c c
b a a
b a b
b a c
b b a
b b b
b b c
b c a
b c b
b c c
c a a
c a b
c a c
c b a
c b b
c b c
c c a
c c b
c c c
27 variations

I created this to work with words... inputs like "bread, breath, blobs, backwards". Be careful because you can quickly get millions of outputs:

1 to the power of 1 is 1
2 to the power of 2 is 4
3 to the power of 3 is 27
4 to the power of 4 is 256
5 to the power of 5 is 3125
6 to the power of 6 is 46,656
7 to the power of 7 is 823,543
8 to the power of 8 is 16,777,216
etc...

Also posted in Math, misc | Tagged , | Leave a comment

MYSQL SOUNDEX

C:
  1. SELECT * FROM _users WHERE SOUNDEX(name) LIKE SOUNDEX('jon');

Not ActionScript, but the coolest thing I've seen in Mysql in awhile, the soundex function will convert a string into a soundex index... read more about it on wikipedia.

I did some tests and it matches things like:

what's your name? & whats yer name

very cool... since this isn't ActionScript, I will post another snippet right after this.

UPDATE: Actually started reading more about this stuff and dug up some algorithms... metaphone etc... fun stuff, maybe I'll port some of it to actionscript...

Also posted in misc, strings | Leave a comment

split() hacks & RegExp

Actionscript:
  1. var path:String = "section/home.swf";
  2.  
  3. var deepLinkValue:String = path.split("/")[1].split(".swf")[0];
  4.  
  5. trace(deepLinkValue);
  6. /*outputs:
  7. home
  8. */

As a primarily self taught programmer there was a time when I didn't know what a regular expression was. There was also a time when ActionScript didn't have them (you had to use a library). Sometimes when I'm in a rush I still do weird things like the above instead of regular expressions... pretty nasty I know.

Here are two different regular expressions that do the same thing:

Actionscript:
  1. var path = "section/home.swf"
  2.  
  3. trace(path.match(/\w+(?=\.)/))
  4. /*outputs:
  5. home
  6. */
  7.  
  8. trace(path.replace(/.+\/|\..+/g, ""))
  9. /* also outputs:
  10. home
  11. */

Actually, I'd be curious to see other regular expressions that only return one value (not an array) that achieve this same thing... feel free to post of a comment if you can think of one...

Also posted in strings | Tagged , | Leave a comment

substr() Remove First Character

Actionscript:
  1. var someString:String = "_file";
  2. trace(someString.substr(1));
  3. /*
  4. ouptuts:
  5. file
  6. */

Turns out the second argument of substr() is optional..... not sure how I never noticed that before.

Also posted in strings | Tagged , | Leave a comment