if statements and textinput value - actionscript-3

If I have a textfield, and I want to use an IF statement to check that text field, for example, I can do this
if (thistxt.text=="query")
{
thisbool = "true";
}
Now let's say I want to use an IF statement to call upon that same textfield but I don't want to pull that entire phrase, (query) maybe just the beginning or end of it, how could I do something like that? Let's say I want to activate the IF statement if that textfield contains or ends with "ery" but is not necessary perfectly equal to "ery".

TextField.text returns a String. Strings have the indexOf() method which returns the position of a substring if it is found, otherwise returning -1. Meaning you can do this:
if (thistxt.text.indexOf('ery') >= 0) {
thisbool = true;
}
There is also the more advanced match() method that uses either strings or regular expressions:
if (thistxt.text.match('ery').length > 0) {
thisbool = true;
}
To match strings at the beginning or end of your input you have to use regex. Fortunately the pattern for these types of matches is trivial when compared to the full capabilities of regex - something like:
if (thistxt.text.match(/^ery/).length > 0) // Match at the start.
if (thistxt.text.match(/ery$/).length > 0) // Match at the end.

Related

Can I include conditional logic in VS Code snippets?

I would like to write a snippet in VS Code that writes a "switch" expression (in Javascript), but one where I can define the number of cases.
Currently there is a snippet that produces the outline of a switch expression with 1 case, and allows you to tab into the condition, case name, and the code contained within.
I want to be able to type "switch5" ("5" being any number) and a switch with 5 cases to be created, where I can tab through the relevant code within.
I know the snippets are written in a JSON file, can I include such conditional logic in this, or is it not possible?
Thanks!
The short answer is that you cannot do that kind of thing in a standard vscode snippet because it cannot dynamically evaluate any input outside of its designated variables with some limited workarounds like I'll mention next.
You might - I and others have written answers on SO about his - type your various case values first and then trigger a snippet tat would transform them into a switch statement. It is sort of doing it backwords but it might be possible.
There are extensions, however, that do allow you to evaluate javascript right in a snippet or setting and output the result. macro-commander is one such extension. I'll show another simpler extension doing what you want: HyperSnips.
In your javascript.hsnips:
snippet `switch(\d)` "add number of cases to a switch statement" A
``
let numCases = Number(m[1]) // 'm' is an array of regex capture groups
let caseString = ''
if (numCases) { // if not 'switch0'
let tabStopNum = 1
caseString = `switch (\${${tabStopNum++}:key}) {\n`
for (let index = 0; index < m[1]; index++) {
caseString += `\tcase \${${tabStopNum++}:value}:\n\t\t\$${tabStopNum++}\n`
caseString += '\t\tbreak;\n\n'
}
caseString += '\tdefault:\n'
caseString += '\t\tbreak;\n}\n'
}
rv = `${caseString}` // return value
``
endsnippet
The trickiest part was getting the unknown number of tabstops to work correctly. This is how I did it:
\${${tabStopNum++}:key}
which will resolve to ${n:defaultValue} where n gets incremented every time a tabstop is inserted. And :defaultValue is an optional default value to that tabstop. If you don't need a defaultValue just use \$${tabStopNum++} there.
See https://stackoverflow.com/a/62562886/836330 for more info on how to set up HyperSnips.

Custom Condition Formatting, how to work with a String amount based condition in a Cell

I want to format cells with a custom function.
The cells have the content like so (without the ") :
Cell_1: "String1 String2 String3"
Cell_2: ""
Cell_3: "String1 String2 String3 String4 String5 String6"
if there is at least like 5 Strings in it, separated with a space it should be a true statement.
I tried to use a custom script like so, because im calling a function in this cell anyway, before returning the value i could set the format too:
var self = SpreadsheetApp.getActiveSheet().getActiveCell();
self.setFontWeight("normal");
if(returnValue.length>=5){
self.setFontWeight("bold");
}
return returnValue.sort().join(" ");
But i got a "You do not have permission to call setFontWeight" Exeption. After a little research i found that cunstom scripts are not allowed to cange format at all. Fine ... but how can i do it with the custom formatting condition
is something like this possible?:
=If(Split(?inputRange?;" ").length>=5;True;False)
If Yes how do i get the Range in the right format and which function is needed to get the String amount?
i tried also to write a custom function for that purpose:
function customFormat(input){
if(input.length>=5)
return true;
}
return false;
calling like so as custom condition:
=customFormat(range)
it seams like i cant use custom function there, i get no positiv result even than i return true always.
If you got some ideas how to accomplish this, i would be thankfull.
Assuming those three cells are in A1:A3
Apply conditional formatting to that range with the formula
=COUNTA(SPLIT(A1, " ")) >= 5

text field greater than or equal to having weird effects in AS3

I have a trigger set like below
var thiseffect:Boolean = false;
if (thistx.text >="6" && thistx.text <="12")
{ thiseffect = true; }
and the trigger will not activate in this case however if I change the 12 value in this trigger to a value below 10, OR if I change the 6 value to something greater than 10 it will trigger with no problem
Im not really sure why that is, has anyone encountered this before?
This isnt exactly an answer but rather a solution
I have converted my text input into a number variable and the trigger activates with no problem now
var thiseffect:Boolean = false;
var mynum:Number = Number(thistx.text);
if (mynum>=6 && mynum<=12)
{ thiseffect = true; }
You can use the following operators to compare strings: <, <=, !=, ==, =>, and >.
But You should note: When using these operators with strings, ActionScript considers the character code value of each character in the string, comparing characters from left to right.
So in your example it compares left to right character by character not by the actual integer value.
trace("12" <= "6") ;//evaluates true
trace("12" <= "06");//evaluates false
refer to Adobe Doc files here.

LINQ-to-SQL oddity with multiple where clause arguments on the same field

My problem requires me to dynamically add where clauses to a IQueryable based on user input. The problem i'm having is that Linq-to-SQL doesn't seem to like having multiple where clauses on the same field, it actually duplicates the search arg value for the last item on all parameters. I verified this behavior through a SQL trace. Here is what I'm seeing.
WHERE ([t22].[OpenText] LIKE #p11) AND ([t22].[OpenText] LIKE #p12)
-- #p11: Input NVarChar (Size = 10; Prec = 0; Scale = 0) [%classify%] // Should be 2da57652-dcdf-4cc8-99db-436c15e5ef50
-- #p12: Input NVarChar (Size = 10; Prec = 0; Scale = 0) [%classify%]
My code uses a loop to dynamically add the where clauses as you can see below. My question is how do I work around this? This pretty much seems like a bug with the tool, no?
// add dyanmic where clauses based on user input.
MatchCollection searchTokens = Helper.ExtractTokensWithinBracePairs(filterText);
if (searchTokens.Count > 0)
{
foreach(Match searchToken in searchTokens)
query = query.Where((material => material.OpenText.Contains(searchToken.Value)));
}
else
{
query = query.Where((material => material.OpenText.Contains(filterText)));
}
Closing over the loop variable considered harmful! Change
foreach(Match searchToken in searchTokens) {
query = query.Where(
material => material.OpenText.Contains(searchToken.Value)
);
}
to
foreach(Match searchToken in searchTokens) {
Match token = searchToken;
query = query.Where(
material => material.OpenText.Contains(token.Value)
);
}
You are closing over the loop variable, which is considered harmful. To fix do this:
foreach(Match searchToken in searchTokens)
{
Match searchToken2 = searchToken;
// ^^^^^^^^^^^^ copy the value of the reference to a local variable.
query = query.Where(material => material.OpenText.Contains(searchToken2.Value));
// use the copy here ^^^^^^^^^^^^
}
The reason why your version doesn't work is that the query refers to the variable searchToken, not the value it had when the query was created. When the variable's value changes, all your queries see the new value.
I don't have enough rep to leave comments yet (or this would be a comment and not an answer) but the answers listed here worked for me.
However, I had to turn off compiler optimizations in order for it to work. If you do not turn off compiler optimizations (at least at the method level) then the compiler sees you setting a loop variable to a local variable and throws the local variable away.

action script 3.0 a string variable that should have only number

In action script var x:String="123abc" I have to check any character, for that string.
i.e. here "abc" is that string so I give an alert that this string should contain only numbers.
How can I do that?
Do you mean to say that you would like to dispatch an alert if a string contains letters
var testVar:String = '123abc';
var pattern:RegExp = /[a-zA-Z]/g;
if( testVar.search(pattern) == -1 )
{
//all good there's no letters in here
}
else
{
//Alert, alert, letter detected!
}
the "pattern" variable is a RegularExpression that's adaptable. Here I'm only checking for letters... If you need more control, get more info about RegularExpressions or come back here with the specific filter you'd like to implement.
I think you are looking for Regular Expression support in AS3.
If the user is inputting text via a TextField then you can set the restrict property to limit the characters that can be entered into the textfield:
textFieldInstance.restrict = "0-9";
TextField.restrict documentation:
http://livedocs.adobe.com/flex/3/langref/flash/text/TextField.html#restrict