By Zevan | February 24, 2009
Actionscript:
-
var input:String = "a, b, c";
-
-
var words:Array = input.split(", ");
-
var max:String = "";
-
var maxD:String = (words.length - 1).toString();
-
for (var i:int = 0; i<words.length; i++){
-
max += maxD;
-
}
-
var maxInt:int = parseInt(max,words.length);
-
-
for(i = 0; i<=maxInt; i++){
-
var indices:String = i.toString(words.length);
-
var r:String = "";
-
var k:int=0;
-
for (var j:int = 0; j<indices.length; j++){
-
r += words[parseInt(indices.charAt(j))] +" ";
-
k++;
-
}
-
while(k <words.length) {
-
r = words[0] +" "+ r;
-
k++;
-
}
-
trace(r);
-
}
-
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 actionscript, flash |
By Zevan | February 15, 2009
C:
-
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 |
By Zevan | February 13, 2009
Actionscript:
-
var path:String = "section/home.swf";
-
-
var deepLinkValue:String = path.split("/")[1].split(".swf")[0];
-
-
trace(deepLinkValue);
-
/*outputs:
-
home
-
*/
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:
-
var path = "section/home.swf"
-
-
trace(path.match(/\w+(?=\.)/))
-
/*outputs:
-
home
-
*/
-
-
trace(path.replace(/.+\/|\..+/g, ""))
-
/* also outputs:
-
home
-
*/
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 actionscript, flash |
By Zevan | February 12, 2009
Actionscript:
-
var someString:String = "_file";
-
trace(someString.substr(1));
-
/*
-
ouptuts:
-
file
-
*/
Turns out the second argument of substr() is optional..... not sure how I never noticed that before.
Also posted in strings | Tagged actionscript, flash |