Using variable within a literal - actionscript-3

In my code I need to declare notationArr1 but I'm getting this error: Error #1010: A term is undefined and has no properties.
if ((notationArr[1].length == 2) && ((notationArr[1].charCodeAt(0) >= 97) && notationArr[1].charCodeAt(0) <= 104) && ((notationArr[1].charCodeAt(1) >= 49) && notationArr[1].charCodeAt(1) <= 56)) {
if (pieces.d3.man == "") {
pieces.notationArr[1].man.y = pieces.d4.y;
}
}
Here, pieces is an object.
Edit: More code: http://sudrap.org/paste/text/44915/

One of the many variables in your little code piece was not properly declared and/or initialized. You can only access properties or methods (every time you write something.something, that's the part after the .) on existing Objects, but not if the variable you are trying to access contains null.
EDIT
Having read your longer code piece, there could be several null variables, but your problem is probably what #AsTheWormTurns mentioned in his comment above:
pieces.notationArr[1].man
will try to access an array called notationArr that is a member of pieces, instead of using an evaluation of the content of notationArr[1] to find out which member of pieces to access. It should be:
pieces[notationArr[1]].man

Related

What is the technical term for a programming language's operator evaluation order?

Several procedures such as array destructuring in JavaScript or collection manipulation in Python have prompted me to evaluate an object's property or method to check if it even exists before proceeding, often resulting in the following pattern:
var value = collection.length
if value != null {
if value == targetValue {
/* do something */
}
}
In an attempt to make "cleaner" code I want to do something like:
if value != null && value == targetValue {
/* do something */
}
or with a ternary operator:
var value = collection.length != null ? collection.length : 0
However, I'm never sure if the compiler will stop evaluating as soon as it resolves the first comparison to null, or if it'll keep going and produce an error. I can of course do small unit tests to find out but I'd prefer if I knew the right term to look up in any language's documentation. What is this term, or is it perhaps the same in all languages?
This is known as Short Circuit Evaluation . It's quite consistent between languages.
In most languages, && will only evaluate the second argument if the first was true, and || will only evaluate its second if the first was false.

While Iterator in groovy

I'm trying to create a loop to read, for example, 4200 users from 1000 to 1000 but I can't get it to cut when it reaches the end. I tried it with if, for and I couldn't do it.
I have programmed in JAVA but with Groovy I see that the structure is different.
urlUsers = urlUsers.concat("/1/1000");
List<UserConnectorObject> usersList = null;
while({
gesdenResponse = GesdenUtils.sendHttpRequest(urlUsers, "LOOKUP", null,
request.getMetaData()?.getLogin(), request.getMetaData()?.getPassword());
log.info("Users data in JSON: "+gesdenResponse.getOutput())
usersList = GesdenUtils.fromJSON(gesdenResponse.getOutput(), GesdenConstants.USER_IDENTITY_KEY);
usersList.size() == 10;
log.info("List size in JSON "+usersList.size());
}()) continue
Groovy has lots of loop structures, but it is crucial to separate the regular ones (lang built-ins) and the api functions which take closure as an argument
take closure - no plain way to escape
If you want to iterate from A to B users, you can use, for instance,
(10..20).each { userNo -> // Here you will have all 10 iterations
if ( userNo == 5) {
return
}
}
If something outrageous happens in the loop body and you cannot use return to escape, as loop boddy is a closure (separate function) and this resurn just exits this closure. Next iteration will happen just after.
use regular lang built-in loop structures - make use of break/continue
for (int userNo in 1..10) { // Here you will have only 5 iterations
if (userNo == 5) {
break
}
}
It looks like your closure always return falsy because there is no explicit return, and the last statement evaluated is the call to log.info(String) which returns void.
Use an explicit return or move/delete the log statement.

missing value from json object when writing if condition it's througing an error

my JSON Object is varies based on some conditins. when i am trying to access the value from json object when it is not present it's througing an error message saying that value is not defined. How to avoid such kind of situations??
IntentSlots: '{"cnumberslot":{"name":"cnumberslot","value":"C186206"}}' - condition works perfectly.
IntentSlots: '{"cnumberslot":{"name":"cnumberslot"}}' } - throws an error.
my condition is like below,
if(IntentSlots.cnumberslot.value !== ""){
}
Based on the condition if the property is not available add an empty value to that property because if u leave it empty it will throw that error.
or use different if condition
if(IntentSlots.hasOwnProperty('value') && IntentSlots.cnumberslot.value !== ""){ }

How to use Transactions in cakephp

I write this code, but its not working, it gives this error
Call to a member function commit() on a non-object
Hear is my code
$datasource = $this->Arrear->getDataSource();
$datasource->begin();
if($this->Customar->saveField("total_bake",$amount) && $this->Arrear->save()){
$dataSource->commit();
return $this->redirect(array('action'=>'index'));
}else{
$dataSource->rollback();
$this->Session->setFlash('Data insert Failed','failure');
}
Variables in php(and hence in cakephp as well) are case-sensitive
http://php.net/manual/en/language.variables.basics.php
you have in your first line
$datasource = $this->Arrear->getDataSource();
but you are committing like
$dataSource->commit();
you have the data source assigned to $datasource, but not to $dataSource. The last variable even is not defined, that is why it is showing that error. So, you have to be sure you are using exactly the same variable (with same capitalization) in all places.

Using predicates in Alloy

I am trying to use two predicates (say, methodsWiThSameParameters and methodsWiThSameReturn) from another one (i.e. checkOverriding) but i receive the following error: "There are no commands to execute". Any clues?
I also tried to use functions but with no success, either due to syntax or to functions do not return boolean values.
They are part of a java metamodel specified in Alloy, as i commented in some earlier questions.
pred checkOverriding[]{
//check accessibility of methods involved in overriding
no c1, c2: Class {
c1=c2.^extend
some m1, m2:Method |
m1 in c1.methods && m2 in c2.methods && m1.id = m2.id
&& methodsWiThSameParameters[m1, m2] && methodsWiThSameReturn[m1, m2] &&
( (m1.acc = protected && (m2.acc = private_ || #(m2.acc) = 0 )) ||
(m1.acc = public && (m2.acc != public || #(m2.acc) = 0 )) ||
(#(m1.acc) = 0 && m2.acc != private_ )
)
}
}
pred methodsWiThSameParameters [first,second:Method]{
m1.param=m2.param || (#(m1.param)=0 && #(m2.param)=0)
}
pred methodsWiThSameReturn [first, second:Method]{
m1.return=m2.return || (#(m1.return)=0 && #(m2.return)=0)
}
Thank you for your response, mr C. M. Sperberg-McQueen, but i think i was not clear enough in my question.
My predicate, say checkOverriding, is being called from a fact like this:
fact chackJavaWellFormednessRules{
checkOverriding[]
}
Thus, i continue not understanding the error: "There are no commands to execute" .
You've defined predicates; they have a purely declarative semantics and they will be true in some subset of instances of the model and false in the complementary subset.
If you want the Analyzer to do anything, you need to give it an instruction; the instruction to search for an instance of a predicate is run. So you'll want to say something like
run methodsWithSameParameters for 3
or
run methodsWithSameParameters for 5
run methodsWithSameReturn for 5
Note that you can have more than one instruction in an Alloy model; the Analyzer lets you tell it which to execute.
[Addendum]
The Alloy Analyzer regards the keywords run and check (and only them) as 'commands'. From your description, it sounds very much as if you don't have any occurrences of those keywords in the model.
If all you want to do is to see some instances of the Alloy model (to verify that the model is not self-contradictory), then the simplest way is to add something like the following to the model:
pred show {}
run show for 3
Or, if you already have a named predicate, you could simply add a run command for that predicate:
run checkOverriding
But without a clause in the model that begins with either run or check, you do not have a 'command' in the model.
You say that you have defined a predicate (checkOverriding) and then specified in a fact that that predicate is always satisfied. This amounts to saying that the predicate checkOverriding is always true (and might just as well be done by making checkOverriding a fact instead of a predicate), but it has a purely declarative meaning, and it does not count as a "command". If you want Alloy to find instances of a predicate, you must use the run command; if you want Alloy to find counter-examples for an assertion, you must use the check command.