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...

Posted in string manipulation, 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.

Posted in string manipulation, strings | Tagged , | Leave a comment

Is This swf Online?

Actionscript:
  1. private var _correctPath:String;
  2.  
  3. //.... somewhere a little later
  4.  
  5.  if (root.loaderInfo.url.split("http://").length == 1){
  6.         _correctPath = "http://www.mywebsite.com/this/is/an/absolute/path/";
  7.     }else{
  8.         _correctPath = "";
  9.     }

I like to do something like this when I'm working locally... it automatically tells my swf to use an absolute path for php files, xml files etc.... then, when I upload it, the LoaderInfo.url property contains an "http://" so it uses a relative path for all the external files. This is common especially if you don't have php and mysql installed on your machine and need to test these using your server.

Posted in misc | Tagged , | 4 Comments

The Big init() #2

Actionscript:
  1. // see previous post for details, this code won't run on it's own
  2. with(addChild(window)){
  3.     x = 10, y = 10;
  4.     with(addChild(c0)) x = 10, y = 10;
  5.     with(addChild(c1)) x = 20, y = 10;
  6.     with(addChild(box)){
  7.         x = 50, y = 10;
  8.         with(addChild(txt)) x = 5, y = 5;
  9.     }
  10. }

It's entertaining to try and think of different DisplayObject nesting techniques. I have a few more of these rolling around in my head that I'll try out soon.... The only good/notable thing about the above is that it shows that nested with statements actually work like you'd expect them to....

Posted in misc | Tagged , | Leave a comment