28.9.10

Regular Expressions

I got down to business again yesterday and I began making scripts in Actionscript 3.0. Here is the script that gave me the error:

var searchForCorn:RegExp = /corn/gi;
 var themTharHills:String = "hillscornhillshillsCornhills";

var result:Object = searchForCorn.exec(themTharHills);

if (result) trace ("There's corn in them thar hills!");

trace(result.index);

result = searchForCorn.exec(themtharHills);

trace(result.index)

result = searchForCorn.exec(themTharHills);

if (result) trace ("There's corn in them thar hills!")

else

trace ("nope")
------------------------------------------------------------------------
-----------------------------
When I ran this script I got an error message, which read:

Scene 1, Layer 'Layer 1', Frame 1, Line 10 1120: Access of undefined property themtharHills.
Don't be alarmed, my fellow Flash CS5 and Actionscript 3.0 users. When you see this error message, the first thing you need to do is check the name- of the property in question of course. You probably just misspelled the property name when you used it again in subsequent sections of the program. Search the code and see if you can find the inconsistency.

Here is the script which has been corrected and should work now.


var searchForCorn:RegExp = /corn/gi;
var themTharHills:String = "hillscornhillshillsCornhills";
var result:Object = searchForCorn.exec(themTharHills);
if (result) trace ("There's corn in them thar hills!");
trace(result.index);
result = searchForCorn.exec(themTharHills);
trace(result.index);
result = searchForCorn.exec(themTharHills);
if (result) trace ("There's corn in them thar hills!");
else 
trace ("nope")

1 comment:

  1. I bet this wouldn't happen if our OSes had more dimensions.

    ReplyDelete