These forums are CLOSED. Please visit the new forums HERE
Test for valid key? |
|
|
Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
|
08-12-2007 12:12
Is there a simple way to test if a given string is in the format of a valid key. I suppose I could write a simple function, but I'm running out of memory in my script and was hoping for a simple low memory use way to check.
|
|
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
|
08-12-2007 14:37
Neat trick from Strife:
http://lslwiki.net/lslwiki/wakka.php?wakka=key CODE
Properly formatted keys (except NULL_KEY) evaluate as TRUE, and invalid ones and NULL_KEY are FALSE. |
|
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
|
08-12-2007 14:39
Neat trick from Strife: http://lslwiki.net/lslwiki/wakka.php?wakka=key CODE
Properly formatted keys (except NULL_KEY) evaluate as TRUE, and invalid ones and NULL_KEY are FALSE. |
|
Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
|
08-12-2007 16:17
A couple of things about this.
The essence of it seems to be in the line : if(in) return 2; It's not clear to me why that works. It clearly only evaluates to true if "in" is a valid key and false if anything else is passed, even if the invalid string is typecast as a key. It's as if the if statement is programmed to recognize valid keys all by itself. Guess it's one of those undocumented "features" Hats off to Strife. I would never have thought of this in a million years. Although returning 2 does no harm, you could also return 1 to stay consistent with the 0=FALSE AND 1=TRUE, convention. Finally I don't see the need to store null_key = NULL_KEY as a global. The functions seems to work just fine in my tests if you just put return (in == NULL_KEY); |
|
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
|
08-13-2007 01:09
Although returning 2 does no harm, you could also return 1 to stay consistent with the 0=FALSE AND 1=TRUE, convention. edit to add: This comes up lots of places you might not expect -- for example, the result of ("a" != "b" is -1, not 1.Finally I don't see the need to store null_key = NULL_KEY as a global. The functions seems to work just fine in my tests if you just put return (in == NULL_KEY); |
|
Monica Balut
Beam-Me
Join date: 18 Feb 2007
Posts: 311
|
08-13-2007 04:59
Thanks. This has been very helpful. Your point about ("a" != "b"
is -1, not 1 is well taken. It suggests that one should not take things for granted in lsl, but should test out what the comparison actually is returning before blindly using it. |