Evaluating equality in perl using elements taken from an array ref - arrays

I have a small perl script that needs to evaluate the equality of two parameters and a small return from the database.
my ($firstId, $secondId, $firstReturnedId, $secondReturnedId, $picCount);
my $pics = $dbh->prepare(qq[select id from pictures limit 10]);
$firstId = q->param('firstId');
$secondId = q->param('secondId');
$pics->execute or die;
my $picids = $pics->fetchall_arrayref;
$picCount = scalar(#{$picids});
$firstReturnedId = $picCount > 0 ? shift(#{$picids}) : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids}) : $firstReturnedId;
Here, a quick look at my debugger shows that $picCount = 1 and $firstReturnedId = 9020 and $secondReturnedId = 9020. However, they are both denoted as
ARRAY(0x9e79184)
0 9020
in the debugger so when I perform the final check
my $result = (($firstId == $firstReturnedId) && ($secondId == $secondReturnedId)) ? 1 : 0;
I get $result = 0, which is not what I want.
What am I doing wrong?

DBI::fetchall_arrayref returns a reference to a list of "row results". But since there could be more than one value in a row result (e.g., your query could have been select id,other_field from pictures), each row result is also a reference to a list. This means you have one more dereferencing to do in order to get the result you want. Try:
$picCount = scalar(#{$picids});
if ($picCount > 0) {
my $result = shift #{$picids};
$firstReturnedId = $result->[0];
} else {
$firstReturnedId = 0;
}
if ($picCount > 1) {
my $result = pop #{$picids};
$secondReturnedId = $result->[0];
} else {
$secondReturnedId = $firstReturnedId;
}
or if you still want to use a concise style:
$firstReturnedId = $picCount > 0 ? shift(#{$picids})->[0] : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids})->[0] : $firstReturnedId;

Related

I want to solve this query with c program

Can someone guide me how to put this properly in an if-else statement.
Consider the following if statement, where doesSignificantWork, makesBreakthrough, and nobelPrizeCandidate are all boolean variables:
if (doesSignificantWork) {
if (makesBreakthrough)
nobelPrizeCandidate = true;
else
nobelPrizeCandidate = false;
}
else if (!doesSignificantWork)
nobelPrizeCandidate = false;
First, write a simpler if statement that is equivalent to this one. Then write a single assignment statement that does the same thing.
if (doesSignificantWork) {
if (makesBreakthrough)
nobelPrizeCandidate = true;
else
nobelPrizeCandidate = false;
}
else if (!doesSignificantWork)
nobelPrizeCandidate = false
Is equivalent to
nobelPrizeCandidate = (doesSignificantWork && makesBreakthrough);
You can make a truth table. The first step is to identify inputs, and write down all combinations of their values.
Input Output
d m n
0 0 ?
0 1 ?
1 0 ?
1 1 ?
Then fill the correct output values
Input Output
d m n
0 0 0
0 1 0
1 0 0
1 1 1
You should now see that the output function corresponds to logical AND (&&).
A simpler if statement is:
if (doesSignificantWork && makesBreakthrough)
nobelPrizeCandidate = true;
else
nobelPrizeCandidate = false;
#Blaze's answer gives you the simplest one-liner. An alternative is
nobelPrizeCandidate = (doesSignificantWork && makesBreakthrough) ? true : false;

Not getting the last item in the list model in xamarin

i am using this method with the list as below to retrieve the Jason list of items in the model
private List<ResturentPair> GetDishItems(List<Resturent> list)
{
var res = new List<ResturentPair>();
for (int i = 0; i < list.Count ; i++)
{
res.Add(new ResturentPair {Item1 = (i < list.Count - 1 ? list[i] : null) , Item2 = ((i < list.Count - 1 && i + 1 < list.Count - 1 )? list[i+1] : null) , Item3 = ((i < list.Count - 1 && i + 2 < list.Count - 1 )? list[i+2] : null)});
i = i + 2;
}
return res;
}
the List<ResturentPair> is collection of food items where the method returns from item 1 to the end but always the method returns with 1 item less than the number of items in the collection , for example say List<ResturentPair> contains items of 10 , it returns only 9 , is that the issue with the for loop or the variable i, help will be appreciated, Thank you in advance for the support
the key is that:
Item1 = (i <= list.Count - 1 ? list[i] : null)
instead of
Item1 = (i < list.Count - 1 ? list[i] : null)
You should do a test for you logic part area, just simply report by using
var firstLogic = (i < list.Count - 1 ? list[i] : null) ;
var secondLogic = i < list.Count - 1 && i + 1 < list.Count - 1;
var thirdLogic = i < list.Count - 1 && i + 2 < list.Count - 1;
First loop
Second loop

Merge random elements of array/split into chunks

How can I split array into chunks with some special algorithm? E.g. I need to shorten array to the size of 10 elements. If I have array of 11 elements, I want two next standing elements get merged. If I have array of 13 elements, I want three elements merged. And so on. Is there any solution?
Sample #1
var test = ['1','2','3','4','5','6','7','8','9','10','11'];
Need result = [['1'],['2'],['3'],['4'],['5|6'],['7'],['8'],['9'],['10'],['11']]
Sample #2
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13'];
Need result = [['1|2'],['3'],['4'],['5'],['6'],['7|8'],['9'],['10'],['11'],['12|13']]
Thank you in advance.
The following code most probably does what you want.
function condense(a){
var source = a.slice(),
len = a.length,
excessCount = (len - 10) % 10,
step = excessCount - 1 ? Math.floor(10/(excessCount-1)) : 0,
groupSize = Math.floor(len / 10),
template = Array(10).fill()
.map((_,i) => step ? i%step === 0 ? groupSize + 1
: i === 9 ? groupSize + 1
: groupSize
: i === 4 ? groupSize + 1
: groupSize);
return template.map(e => source.splice(0,e)
.reduce((p,c) => p + "|" + c));
}
var test1 = ['1','2','3','4','5','6','7','8','9','10','11'],
test2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
console.log(condense(test1));
console.log(condense(test2));
A - Find the difference and create thus many random numbers for merge and put in array
B - loop through initial numbers array.
B1 - if iterator number is in the merge number array (with indexOf), you merge it with the next one and increase iterator (to skip next one as it is merged and already in results array)
B1 example:
int mergers[] = [2, 7, 10]
//in loop when i=2
if (mergers.indexOf(i)>-1) { //true
String newVal = array[i]+"|"+array[i+1]; //will merge 2 and 3 to "2|3"
i++; //adds 1, so i=3. next loop is with i=4
}
C - put new value in results array
You can try this code
jQuery(document).ready(function(){
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'];
var arrays = [];
var checkLength = test.length;
var getFirstSet = test.slice(0,10);
var getOthers = test.slice(10,checkLength);
$.each( getFirstSet, function( key,value ) {
if(key in getOthers){
values = value +'|'+ getOthers[key];
arrays.push(values);
}else{
arrays.push(value);
}
});
console.log(arrays);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Biopython for Loop IndexError

I get "IndexError: list is out of range" when I input this code. Also, the retmax is set at 614 because that's the total number of results when I make the request. Is there a way to make the retmode equal to the number of results using a variable that changes depending on the search results?
#!/usr/bin/env python
from Bio import Entrez
Entrez.email = "something#gmail.com"
handle1 = Entrez.esearch(db = "nucleotide", term = "dengue full genome", retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
while i >= 0 and i <= len(IdNums):
handle2 = Entrez.esearch(db = "nucleotide", id = IdNums[i], type = "gb", retmode = "text")
record = Entrez.read(handle2)
print(record)
i += 1
Rather than using a while loop, you can use a for loop...
from Bio import Entrez
Entrez.email = 'youremailaddress'
handle1 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
for i in IdNums:
print(i)
handle2 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', id = i, rettype = 'gb', retmode = 'text')
record = Entrez.read(handle2)
print(record)
I ran it on my computer and it seems to work. The for loop solved the out of bounds, and adding the term to handle2 solved the calling error.

Matlab not solving if statements

I'm having a bit of trouble iterating through a few if statements more than eight times. The code seems to work fine for the first several comparisons, performs the arithmetic and return/saves the output row 'export_data'. However, after that, it only returns the else condition and response. The variables beings assessed have 1500 rows each. I've added the code below and two photos showing the outputs. Any insight will be very much appreciated.
function [export_data] = WS_Zones(Forecast_WS, Observed_WS)
if (Forecast_WS > Observed_WS)
WS_Zone_1 = Observed_WS.*1.24;
WS_Zone_2 = Observed_WS.*1.28;
elseif (Forecast_WS < Observed_WS)
WS_Zone_1 = Observed_WS.*0.76;
WS_Zone_2 = Observed_WS.*0.72;
else
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
end
export_data=[Forecast_WS Observed_WS WS_Zone_1 WS_Zone_2];
filename = 'testdata.xlsx';
sheet = 1;
xlRange = 'A1';
xlswrite(filename,export_data,sheet,xlRange)
end
Expected Output
Wrong Output
This statement:
if [1 2 3] > [1 1 1]
disp('hello');
end
will never print "hello" even though 2 and 3 are both greater than 1. This is because the if statement needs to evaluate to either scalar true or false. If a vector is used, than only the first element is used to determine if the statement is true or not (comparisons between other elements are ignored). You can use any and all if you want to apply conditions on all elements.
If Forecast_WS and Observed_WS aren't scalars then you need to wrap your if statement in a for loop, e.g.:
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
for i = 1:numel(Forecast_WS)
if Forecast_WS(i) > Observed_WS(i)
WS_Zone_1(i) = Observed_WS(i).*1.24;
WS_Zone_2(i) = Observed_WS(i).*1.28;
elseif Forecast_WS(i) < Observed_WS(i)
WS_Zone_1(i) = Observed_WS(i).*0.76;
WS_Zone_2(i) = Observed_WS(i).*0.72;
end
end
or vectorize it using logical indexing:
WS_Zone_1 = Observed_WS;
WS_Zone_2 = Observed_WS;
idx = (Forecast_WS > Observed_WS);
WS_Zone_1(idx) = Observed_WS(idx).*1.24;
WS_Zone_2(idx) = Observed_WS(idx).*1.28;
idx = (Forecast_WS < Observed_WS);
WS_Zone_1(idx) = Observed_WS(idx).*0.76;
WS_Zone_2(idx) = Observed_WS(idx).*0.72;

Resources