7 foreach (#crons) {
8 unless (index($_, "cks") != -1) {
9 unless (index($_, "aab") != -1) {
10 unless (index($_, "lam") != -1) {
11 push (#found, $_);
12 }
13 }
14 }
15 }
how come the above does not give the same output as the following:
7 foreach (#crons) {
8 unless (index($_, "cks") != -1 && index($_, "aab") != -1 && index($_, "lam") != -1) {
9 push (#found, $_);
10 }
11 }
#crons has the list of strings and I am trying to get all string that does not have "cks", "aab" and "lam"
The first section of code does what i want but the second doesnt and in my mind, it should...
Can anyone care to explain why they are not the same nor why it does not give the same output?
Let's call your conditions A, B, C. We now have the code
unless (A) {
unless (B) {
unless (C) {
The unless can be very confusing, so we write it only using if:
if (!A) {
if (!B) {
if (!C) {
Now we && those conditions together:
if (!A && !B && !C) {
This could also be written as
if (not(A || B || C)) {
This equivalence is called de Morgan's law
The non-equivalence of the two logics become clear when you test the string 'cks'.
The first logic would evaluate to false, the second would evaluate to true, since it does not contain the string 'aab' or 'lam'.
Related
I am looking to use two conditions together to handle a display. If both a and b are true, then show something.
In JavaScript, we would write: if (a && b) { ... }
Moustache
{{#(one && two)}} // or #one && #two
<p>foobar</p>
{{/(one && two)}}
JSON
{
"one": "OK",
"two": "Yes"
}
Remember that
if (a && b) {
}
is the same as
if (a) {
if (b) {
}
}
So you can write you Mustach template like this:
Moustache
{{#one}}
{{#two}}
<p>foobar</p>
{{/two}}
{{/one}}
I have a product table with 20 column. There is a column 'Width' which is decimal data type. I have been calling particular width value in web api controller to populate results. As an example,width values are like below:
Width values
1 0.0015
2 1.0000
3 0.0063
4 1.0100
5 2.0000
6 2.0630
public HttpResponseMessage GetclassByWidthList(Decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
**var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && **u.Width == cWidth**) select u.Class).Distinct().ToList()**;
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
method is working fine. results are reflected if width value is "1.0000" OR "2.0000" but for other values of width it is not populating any result.
Please help me on above query where other decimal values with precision will populate result.
i have tried in browser to populate result:
1.
http://localhost:55481/api/KendoCascading/GetclassByWidthList/1 - result is poupulating
`http://localhost:55481/api/KendoCascading/GetclassByWidthList/1.01 - http-404 error showing
For debugging, your decimal data of Cwidth is being truncated, Please check
As per Ehasanul's suggestion i Just modified my method with putting extra "/" last of that route path
[Route("**api/KendoCascading/GetclassByWidthList/{cWidth:decimal}/**")]
public HttpResponseMessage GetclassByWidthList(decimal cWidth)
{
using (CrossReferenceTool1Entities cls1 = new CrossReferenceTool1Entities())
{
var query = (from u in cls1.Products where (u.PrivateOnly == false && u.SelectionTool == true && u.ProductTypeID == 2 && (u.ProductFamilyID == 11 || u.ProductFamilyID == 12 || u.ProductFamilyID == 58 || u.ProductFamilyID == 59 || u.ProductFamilyID == 92) && u.Width == cWidth) select u.Class).Distinct().ToList();
HttpResponseMessage res;
res = Request.CreateResponse(HttpStatusCode.OK, query);
return res;
}
}
call that url in my angular service http.get method with adding extra "/" in last.
this.getclsWidthList3 = function (cWidth) {
var res;
if (cWidth !== 0.0000) {
res = $http.get("/api/KendoCascading/GetclassByWidthList"+ "/"+ cWidth+"/");
return res;
}
};
and its working great.
I have some piece of code for multiple conditions in Perl
if (/abc/ && !/def/ && !/ghi/ && jkl) {
#### do something
}
Will every condition will be evaluated at once on every line?
I can prioritize the conditions using nested ifs
if (/abc/){
if (!/def/){
....so on
}
&& short-circuits. It only evaluates its RHS operand if needed. If it's LHS operand returns something false, && will that value.
For example,
use feature qw( say );
sub f1 { say "1"; 1 }
sub f2 { say "2"; 0 }
sub f3 { say "3"; 0 }
sub f4 { say "4"; 0 }
1 if f1() && f2() && f3() && f4();
Output:
1
2
So the following two lines are basically the same:
if (/abc/) { if (!/def/) { ... } }
if (/abc/ && !/def/) { ... }
In fact, if compiles into an and operator, so the above are very close to
(/abc/ and !/def/) and do { ... };
(/abc/ && !/def/) and do { ... };
No.
Think of it like this, if I said
"is the moon bigger than the sun?"
AND "is the pacific bigger than the mediterraan?"
AND "is russia bigger than england?"
AND ... many more AND ....
You could answer "no" very quickly, not having to figure out the answer to anything beyond the first question. It's called "short circuiting"
So in your case, unless an input line matches
/abc/ && !/def/ && !/ghi/
You won't need to evaluate whether it matches /jkl/.
Let's say we have a description field on my form with optional check boxes. The check boxes represent which fields to search when doing the lookup. Right now I have a matrix of look ups that call their unique version of where clause. It works but I think it smells a bit.
Here is an excerpt
// Look for part numbers decide how many fields to search and use that one.
// 0 0 X
if (!PartOpt[0] && !PartOpt[1] && PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum)));
}
// 0 X 0
if (!PartOpt[0] && PartOpt[1] && !PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt.Contains(partSearchRec.inventory.PartNum)));
}
// 0 X X
if (!PartOpt[0] && PartOpt[1] && PartOpt[2])
{
query = query.Where(p => (p.PartNumAlt.Contains(partSearchRec.inventory.PartNum)
|| p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum)));
}
// X 0 0
if (PartOpt[0] && !PartOpt[1] && !PartOpt[2])
{
query = query.Where(p => (p.PartNum.Contains(partSearchRec.inventory.PartNum)));
}
. . .
This goes on for a while and seems to be prone to coding errors. In each case we are looking for the same information in any of the selected fields. If I was doing this in SQL I could simply build up the WHERE clause as needed.
Once again I rubber ducked my way to an answer. Rather than throw the question away, here is what I came up with. Is it efficient?
if (partSearchRec.optPartNum || partSearchRec.optAltPartNum1 || partSearchRec.optAltPartNum2)
{
query = query.Where(p => (
(partSearchRec.optPartNum && p.PartNum.Contains(partSearchRec.inventory.PartNum))
|| (partSearchRec.optAltPartNum1 && p.PartNumAlt.Contains(partSearchRec.inventory.PartNum))
|| (partSearchRec.optAltPartNum2 && p.PartNumAlt2.Contains(partSearchRec.inventory.PartNum))));
}
Basically if any of the check boxes are set we will execute the query. Each line of the query will be processed only if the check box was checked. If the left side of an AND is false it doesn't process the right.
This is an aera that Delphi's with statement would be handy. I also learned that you can't use an array inside the LINQ statement.
Given a certain sequence A stored in an array, I have to find if a larger sequence B contains sequence A.
I am stuck at the index part... and i'm getting an error that argument "TGACCA" isn't numeric in array element in line 69 which is:
if (index($record_r1[1], $r2_seq[$check]) != -1)
The code is:
foreach my $check (#r2_seq)
{
if (index($record_r1[1], $r2_seq[$check]) != -1)
{
$matches= $matches + 1;
print "Matched";
}
else
{
}
}
foreach my $check (#r2_seq)
$check takes on the value of each element in #r2_seq. It is not the index.
$r2_seq[$check]
This is attempting to use an element of #r2_seq as the index into #r2_seq. It is unlikely what you want. More probably, you want to use
$check
as in
if (index($record_r1[1], $check) != -1)
.
I believe you wanted $check to be index, so then use the following code:
foreach my $index (0..$#r2_seq)
{
if (index($record_r1[1], $r2_seq[$index]) != -1)
{
$matches= $matches + 1;
print "Matched";
}
else
{
}
}