how to replace a certain value in string? - actionscript-3

I am using Flexbuilder with sdk 3.5. I convert an int to a binary string. Now I want to replace binary value at certain index. How can I do this?
I tried following but it did not work;
binaryStr[0] = "0";
and
binaryStr[0] = '0';

You could use String.split() to convert your String into an Array, then change what you want, and finally convert the resulting Array into a String with Array.join() ?

Related

Identifying variable type in GNU Octave

When practicing with Octave I created a variable with the name my_name = ["Andrew"] and upon asking Octave to interpret whether it was a string it outputted a '0'. Again when using the typeinfo(my_name) I got ans = string. Why am I getting this sort of output?
octave:47> my_name = ["Andrew"]
my_name = Andrew
octave:48> isstring(my_name)
ans = 0
octave:49> typeinfo(my_name)
ans = string
According to the documentation (emphasis mine):
isstring (s)
Return true if s is a string array.
A string array is a data type that stores strings (row vectors of characters) at each element in the array. It is distinct from character arrays which are N-dimensional arrays where each element is a single 1x1 character. It is also distinct from cell arrays of strings which store strings at each element, but use cell indexing ‘{}’ to access elements rather than string arrays which use ordinary array indexing ‘()’.
Programming Note: Octave does not yet implement string arrays so this function will always return false.
That is, isstring will always return false (or 0), no matter what the input is.
You should use ischar to determine if the input is a character array (==string).

LUA, multiple number string to number

so I use web to request json:
{"number":"1,2,3"} OR table = {number="1,2,3"}
and when I used this, it shows number:
typeof(1,2,3)
but when i directly get the data from the json/table, it shows string, so is there anyway to convert it to show it as number?
A Lua pattern might also a good choice to get the numbers from the raw string; then use tonumber() as suggested and add the numbers to a table in the sample code as shown below:
numbers = {}
str = '1,2,3'
for num in string.gmatch(str, '([^,]+)') do
table.insert(numbers, tonumber(num))
end

How to convert jsonAST.Jint to int

I am attempting to learn Scala, and I'm trying to parse a JSON file. I have two lines of code:
var jVal:JValue = parse(json);
val totalCount:Int = (jVal \\ "totalCount").asInstanceOf[Int];
However, (jVal \\ "totalCount") returns a JInt instead of an int. If I print it as a string, it looks like "JInt(38)".
How on earth do I convert this to a regular int? My current code throws an exception saying that
net.liftweb.json.JsonAST$JInt cannot be cast to java.lang.Integer
I've scoured the internet, but I can't find any answers. I would really prefer not to manually parse and remove the "JInt()" part of the string just to get it as an integer.
Surely I am missing a simple way to do this?
Since JInt is a case class, a convenient way to extract the value is using an extractor expression, either in a match:
myJValue match {
case JInt(x) => /* do something with x */
case JString(s) => /* do something with s */
/* etc. */
}
or just an assignment statement, when you know what type to expect:
val JInt(totalCount) = (jVal \\ "totalCount")
This will define totalCount to be the value of "totalCount" in your JSON. Note that it will be of type BigInt. If you want to, you can convert your BigInt to an Int with the toInt method. But if your number is too big for an Int, this method will give you a different number instead of an error. So if huge numbers are at all a possibility, you'll want to check first with isValidInt.
You can also get the value using the num field or values method, but in your code that's harder to work with. To use num, you'd have to do a cast of your JValue to JInt. And if you don't cast to JInt, you won't know the type of the result of values.

Setting lua table in redis

I have a lua script, which simplified is like this:
local item = {};
local id = redis.call("INCR", "counter");
item["id"] = id;
item["data"] = KEYS[1]
redis.call("SET", "item:" .. id, cjson.encode(item));
return cjson.encode(item);
KEYS[1] is a stringified json object:
JSON.stringify({name : 'some name'});
What happens is that because I'm using cjson.encode to add the item to the set, it seems to be getting stringified twice, so the result is:
{"id":20,"data":"{\"name\":\"some name\"}"}
Is there a better way to be handling this?
First, regardless your question, you're using KEYS the wrong way and your script isn't written according to the guidelines. You should not generate key names in your script (i.e. call SET with "item:" .. id as a keyname) but rather use the KEYS input array to declare any keys involved a priori.
Secondly, instead of passing the stringified string with KEYS, use the ARGV input array.
Thirdly, you can do item["data"] = json.decode(ARGV[1]) to avoid the double encoding.
Lastly, perhaps you should learn about Redis' Hash data type - it may be more suitable to your needs.

Is there a function to test if a String variable is a number value?

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.