How to assign an expression to a model? - angularjs

I am having the following set up inside an ng-repeat directive:
<span ng-model="d.attr3">
{{d.attr2 - d.attr1}}
</span>
d is the variable used inside ng-repeat.
My issue is that I expect d.attr3 to hold the value of the expression inside span tag. However, it holds the expression NaN.
Note that I have converted the d.attr1 and d.attr2 values to integers - In fact, the expression evaluates correctly but it is not bound to the scope - how do I get d.attr3 to hold the value of the expression?

<span ng-init="d.attr3 = d.attr2 - d.attr1">
{{d.attr3}}
</span>

Related

In this conditional JSX rendering, why is a double (!!) required?

In the following JSX
Why is there a double !
{!! count && <div className={'styles.info'}>{`did this ${count}`}</div>}
You don't want the space between !! and count. They should be together like so: !!count.
Like the comment said, it is coercing the value to a boolean. If count is a falsey value, say 0, it would still be rendered by React. Using !! to coerce 0 to false, would guarantee that 0 is never rendered.

Selenium: is there a way to write an xpath to get elements that contains text that ends with numbers

I'm trying to write an xpath that selects all the elements that contains text which ends with numbers and surrounded by braces. The numbers can be anything and it can be of any number of digits. Is this possible in XPath1.0? I cannot use regex as it is not allowed in XPath1.0
<div class = '12345' >
<div class = '98231'>I want this (101)</div>
<div class = '98232'>I don't want this 101</div>
<div class = '98233'>I want this (1)</div>
<div class = '98234'>I don't want this (10A1)</div>
</div>
In XPath-1.0 you cannot use RegEx'es.
But you can use the fn:number() function to check if a given string could be a valid number with the fn:boolean function:
/div/div[boolean(number(substring-before(substring-after(.,'('),')')))]
Obviously, this does only work if there are no other brackets in the string. And it doesn't check if the number in brackets is at the end of the string. To add this further restriction, change the expression to
/div/div[not(boolean(string(normalize-space(substring-after(.,')'))))) and boolean(number(substring-before(substring-after(.,'('),')')))]
This adds a further predicate to check if there are characters other than whitespace after the ). Leading and trailing whitespace between the brackets is automatically removed by the fn:number() function.
Try following xpath:
//div//div[text() and contains(text(),'(') and contains(text(),')')]

Swift predicate only matches first value in array of values

I have a class Download that serves as a wrapper for CKQueryOperation. One of the inits allows me to build my predicate with an array of values:
init(type: String, queryField: String, queryValues: [CKRecordValue], to rec: RecievesRecordable, from database: CKDatabase? = nil) {
let predicate = NSPredicate(format: "\(queryField) = %#", argumentArray: queryValues)
query = CKQuery(recordType: type, predicate: predicate)
reciever = rec
db = database
super.init()
}
When I test it, query only matches the first value in the array. So if queryValues = [testValue0, testValue1] and I have one record whose field matches testValue0 and I have a second record that matches testValue1, only the first record will be discovered. If I switch the order, than the other record gets recognized.
It seems weird that I can create a predicate with an array but only the first value gets matched. The documentation says that values are substituted in the order they appear, but shouldn't it still be moving on to the second value?
For more context, each record is stored in a separate database (private vs public) and my Download class launches two separate CKQueryOperations that both rely on query, if database param is left nil. Whichever op fails ends up finding no results that match the first value and then giving up before checking the second value.
I can include the full code for 'Download' and my failing unit test if needed.
You are using the wrong predicate. You want to use the IN operation. And instead of using string substitution for the field name, use the %K format specifier:
let predicate = NSPredicate(format: "%K IN %#", arguments: queryField, queryValues)
Note that the NSPredicate(format:argumentArray:) expects there to be one format specifier in the format string for each value in the argument array. Since your format only had one format specifier, only the first value was taken from the array. And since you used =, the field was simply compared against that one value.
Basic Comparisons
=, == The left-hand expression is equal to the right-hand expression.
=, => The left-hand expression is greater than or equal to the right-hand expression. <=, =< The left-hand expression is less than or
equal to the right-hand expression.
The left-hand expression is greater than the right-hand expression. < The left-hand expression is less than the right-hand expression. !=,
<> The left-hand expression is not equal to the right-hand expression.
String Comparisons
BEGINSWITH The left-hand expression begins with the right-hand
expression.
CONTAINS The left-hand expression contains the right-hand expression.
ENDSWITH The left-hand expression ends with the right-hand expression.
LIKE The left hand expression equals the right-hand expression: ? and
are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.
MATCHES The left hand expression equals the right hand expression
using a regex-style comparison according to ICU v3 (for more details
see the ICU User Guide for Regular Expressions).
I used Basic Comparisons
let resultPredicate = NSPredicate(format: "%K = %#", "key", "value")
let result_filtered = MYARRAY.filtered(using: resultPredicate) as NSArray
More information : Predicate Programming Guide

Array formula in excel suddenly not working... troubleshooting

I am currently using this array formula..
{=LARGE(IF(('Data Input'!$L$3:$L$15000=$B10)*('Data Input'!$H$3:$H$15000>$C10),'Data Input'!$O$3:$O$15000,0),1)}
Where B10 is a text ID, like 658A and L:L is the column with the IDs.
C10 is a date, with H:H being the column with dates.
O:O being the column with the # value that I am retrieving.
This formula works fine with my purposes when used with ctrl,shift,enter
The problem arises when I try to use...
{=IF('Data Input'!$L$3:$L$15000=$B10,1,0)}
It always returns a FALSE result, even though it works correctly in the first formula.
What is different about the second formula that changes the results?
This is very strange to me.
Thanks for any help.
the IF is only comaring the first value of the array that is returned, so only if the first comparison is true, will it return a true value.
Example to illustrate:
formula
Formula:
{=IF(A1:A3=B2,1,0)} will; return 0, unless cell A1 is changed to true. To change the result to have it return true if any of the values are true, you have to resort to a little trickery...
First, use -- to change the True/False values to 1/0, then use SUM to add them together. as IF treats any non-zero result as true, this will result in 1 being returned when any comparison is true.
Working through our example with the new formula {=IF(SUM(--(A1:A3=B2)),1,0)} (still an array formula) we get the following steps in evaluation:
=IF(SUM(--(A1:A3=B2)),1,0)
=IF(SUM(--(A1:A3=2)),1,0)
=IF(SUM(--({1,2,2}=2)),1,0)
=IF(SUM(--({False,True,True})),1,0)
=IF(SUM(0,1,1),1,0)
=IF(2,1,0)
=1
Your second formula is, itself, returning an array. You are only viewing the top left element in that return array - which happens to be FALSE.
Your first formula returns a scalar value; that is the difference.
If you want to sum the '1' values then your second formula could be amended to
{=SUM(IF('Data Input'!$L$3:$L$15000=$B10,1,0))}
which is also a scalar return.

Why doesn't ||= work with arrays?

I use the ||= operator to provide default values for variables, like
$x ||= 1;
I tried to use this syntax with an array but got a syntax error:
#array||= 1..3;
Can't modify array dereference in logical or assignment (||=) ...
What does it mean and how should I provide arrays with default values?
Because || is a scalar operator. If #array||= 1..3; worked, it would evaluate 1..3 in scalar context, which is not what you want. It's also evaluating the array in scalar context (which is ok, because an empty array in scalar context is false), except that you can't assign to scalar(#array).
To assign a default value, use:
#array = 1..3 unless #array;
But note that there's no way to tell the difference between an array that has never been initialized and one that has been assigned the empty list. It's not like a scalar, where you can distinguish between undef and the empty string (although ||= doesn't distinguish between them).
eugene y found this perl.perl5.porters message (the official Perl developers' mailing list) that goes into more detail about this.
This page has a good explanation, imho:
op= can occur between any two
expressions, not just a var and an
expression, but the left one must be
an lvalue in scalar context.
Since #x ||= 42 is equivalent to
scalar(#x) = #x || 42, and you aren't
allowed to use scalar(#x) as an
lvalue, you get an error.

Resources