I am trying the following right now:
function isNum = isItANum(string)
isNum = isempty(str2num(string))
end
The problem is if I have a date '1998/34/2', I want my function to say no.
From help str2num:
*Caution:* As `str2num' uses the `eval' function to do the
conversion, `str2num' will execute any code contained in the
string S. Use `str2double' instead if you want to avoid the use
of `eval'.
See also: str2double, eval
Looks like you can replace your function with ~isnan(str2double(string))
do a loop so that you split the string in single characters, and if any char fail, then return 0.
Related
I have a function that takes a vector as a parameter, scan this vector and generates a random word. It's expected from me that the generated words' letters are different from each other. So, I want to check it with a simple if-else condition inside the same function. If all letters are different, function returns this word. If not, I need to use the same function which I am already inside while using conditions. But first parameter that I used in the main function doesn't work when I attempt to use it for the second time. Here the generateaRandomWord(vector a) function:
vector<string> currentVector;
string generateaRandomWord(vector<string> a) {
currentVector = a;
string randomWord;
int randomNumber = rand() % currentVector.size();
randomWord = currentVector.at(randomNumber);
if (hasUniqueChars(randomWord)) {
return randomWord;
}
else {
generateaRandomWord(currentVector);
}
}
I thought that it is a good idea to keep a vector (currentVector) outside of the function. So, for the first time I use the function this vector will be defined and I will be able to use it if using recursion is necessary. But that didn't work either.
The main problem you have is that your recursive case doesn't return anything -- it throws away the returned value from the recursive call, then falls off the end of the function (returning garbage -- undefined behvaior). You need to actually return the value returned by the recursive call:
return generateaRandomWord(currentVector);
I have a field in SQL Server that contains an comma separated list. Here are 2 examples:
select 'ex1,ex2,ex3' as str union all
select 'ax1,ax2'
In my report, I have to transform all of these values (5 in this case) using a function. In this question I will use Trim, but in actuality we are using another custom made function with the same scope.
I know how I can split every value from the string and recombine them:
=Join(Split(Fields!str.Value,","),", ")
This works great. However, I need to execute a function before I recombine the values. I thought that this would work:
=Join( Trim(Split(Fields!VRN.Value,",")) ,", ")
However, this just gives me an error:
Value of type '1-dimensional array of String' cannot be converted to 'String'. (rsCompilerErrorInExpression)
I can't personally change the function that we use.
How do I use an extra function when dealing with both an split and a join?
You can use custom code to include all the logic (Split->Custom Code->Join).
Make adjustments inside the loop to call your custom function instead of trim
Public Function fixString (ByVal s As String) As String
Dim mystring() As String
mystring = s.Split(",")
For index As Integer = 0 To mystring.Length-1
mystring(index) = Trim(mystring(index))
Next
Return Join(mystring, ",")
End Function
To call the custom code use the following expression
Code.fixString( Fields!VRN.Value )
I'm getting terribly confused by the colon notation used when defining/calling Lua functions.
I thought I'd got my head round it until I saw this piece of code:
function string.PatternSafe( str )
return ( str:gsub( ".", pattern_escape_replacements ) );
end
function string.Trim( s, char )
if char then char = char:PatternSafe() else char = "%s" end
return string.match( s, "^" .. char .. "*(.-)" .. char .. "*$" ) or s
end
What's confusing me here is that string.PatternSafe() doesn't reference 'self' anywhere, yet the code seems to work.
I've also seen some scripts that use colon notation when defining the function, for example:
function foo:bar( param1 ) ... end
After several hours of googling I've still not managed to work out what precisely is happening in these two contexts. My current assumptions are as follows:
If a function is defined using colon notation, it gets an invisible 'self' parameter inserted as first parameter
If a function is called using colon notation, the object preceding ':' is inserted in to the arguments (so becomes the first parameter of the function)
If a function is called using dot notation, then even if it was defined using colon notation it will not get the object inserted as first argument/parameter
If my assumptions are correct, that raises an additional question: What is the best way to ensure that the function was called properly?
Your assumptions are all correct.
Assumption 1 from the manual:
The colon syntax is used for defining methods, that is, functions
that have an implicit extra parameter self. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
Assumption 2 from the manual:
A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.
Assumption 3 doesn't have a direct manual section since that's just normal function call syntax.
Here's the thing though. self is just the auto-magic name given in the syntax sugar used as part of the colon assignment. It isn't a necessary name. The first argument is the first argument whatever the name happens to be.
So in your example:
function string.PatternSafe( str )
return ( str:gsub( ".", pattern_escape_replacements ) );
end
the first argument is str so when the function is called as char:PatternSafe() is de-sugars (via assumption 2) to char.PatternSafe(char) which is just passing char to the function as the first argument (which, as I already said, is str).
I'm working with lua-alchemy, and I'm setting a global variable in my AS3 code in this way:
_lua.setGlobal("map", _map);
With _map being a object with the following function in it:
public function get x():int
{
return 10;
}
if then I try to do something like this in Lua
local a = map.x + 1
I get the following error:
Lua script failed: luaDoString:21: attempt to perform arithmetic on field 'x' (a table value)
Does anyone knows why it does that, and how I could fix it?
EDIT :
When I print type(map.id), it prints table... Shouldn't it print number?
I found my error. According to this page, I have to use as3.tolua(map.x) to convert it to the right type.
Is there a way to test a string, such as the one below to see if it's an actual number value?
var theStr:String = '05';
I want to differentiate between the string value above and one such as this:
var theStr2:String = 'asdfl';
Thanks!
Yes use isNaN function to test if it the String is a valid Number:
var n:Number=Number(theStr);
if (isNaN(n)){
trace("not a number");
} else {
trace("number="+n);
}
You must cast to Number to get is NaN. If you use int letters can be cast to 0.
If you are just interested in checking integers you could use the match function as follows, the regex for numbers is more complicated and you would likely be better off following the casting method Patrick provided.
if (s.match(/^\d+$/)){//do something}
Of course if you are going to need to cast it anyway then using isNaN makes perfect sense. Just thought I'd offer an alternative in case you weren't going to cast it.
This code will return true if s contains only digits (no spaces, decimals, letters etc...) and requires there be at least 1 digit.