I know I can access the last element of an array with
array[end]
I've tried to access the second to last element with
array[end-1] which (unsurprisingly) did not work. I was unable to find anything reading through the documentation or just through googling. How do I access the second to last element? Thanks for any help you can provide.
Edit
It turns out that what I thought was an array was actually a vector. In that case, vector[end - 1] does not work, but that which is suggested below does.
I'm also surprised that doesn't work. Are you sure you tried it on an array with at least two elements?
You could also try using the length() function:
last = array[length(array)]
secondToLast = array[length(array)-1]
Related
I am working on a school assignment where I have to find the smallest hitting set of a list of lists. Right now I am working on finding all the hitting sets before I narrow it down to the smallest one. I implemented a way for my code to find out if two lists have at least one number in common. To my knowledge it works as intended by itself but whenever I try to connect it to the main part of my code it won't work or runs indefinitely. Any help would be appreciated.
Minimal, Reproducible Example:
This code has been made in a way that it will return true anytime a two arrays have at least one number that intersects between the two. I have tested this code and to my knowledge it works as intended. The first result back is always correct when I tested it.
checkForIntersection([],[]):- false.
checkForIntersection([Head|Tail],[Head2|Tail2]):-
Head = Head2;
checkForIntersection(Tail,[Head2|Tail2]);
checkForIntersection([Head|Tail],Tail2).
This part of the code is where I believe an error occurs. I have an AnswerSet as the list that I want to check for intersections. The [Check|NextCheck] is a list of lists and I want to check each of them against the AnswerSet. I loop through it until the [Check|NextCheck] is empty. The issue is that when I call it the results I get is an infinite amount of trues even if the answer should be false.
loopThroughListOfLists(CheckListsAgenstThisList,[]).
loopThroughListOfLists(CheckListsAgenstThisList,[ListToCheck|NextListToCheck]):-
checkForIntersection(ListToCheck,CheckListsAgenstThisList),
loopThroughListOfLists(CheckListsAgenstThisList,NextListToCheck).
loopCheck([3],[[1], [1], [3], [4], [4]]). This is one of the cases I used to test my code with. Instead of returning false it returns an infinite amount of trues whenever I test it.
Thank you so much for reading, I am sorry if this is a really stupid question, I am really struggling with Prolog.
I am trying use simple slice operator as follows, but the result is not correct.
arr = c(1,2,3,4,5,6,7)
arr[2:2+3]
I expected to get the sliced array 2,3,4,5 but instead I get 5. Does R interpret arr[2:2+3] as arr[2:2]+3 ? If so, then why?
The correct version of slice would be arr[2:(2+3)], right?
As #camille pointed out in the comment section, the reason of failure is due to the order of operations!
My -layout is not as expected:
Why does .getstring create a single cell at the end of the table?
Is this a bug?
Can you fix this via getrow and join?
Thanks for your tips
Getstring-Example# http://www.w3schools.com
It's a known... well, annoyance. (Otherwise known as a bug that will never get fixed.) You either have to trim off the extra delimiters using Left(), or you have to use something other than .GetString. .GetRows is probably the most useful, but note that you can't use Join with it: the latter requires a one-dimensional array, while GetRows always returns a two-dimensional array, even if the recordset only returned one column and/or row.
Today I had a little problem with excelphp.
I wanted to set the default width of the columns manually and only one single time.
I found a few solutions also here on stackoverflow which suggested to change the value for every single column (e.g. within a loop).
But I wanted to do it with a single command.
After browsing the source code I found out the following solution:
$phpExcelObject->getActiveSheet()->getDefaultColumnDimension()
->setWidth($myCustomWidth);
I hope that this helps anybody else ;)
on the forum I found the ?which() command and applied it on a vector containing a sequence of time indexed values as follows:
first.element <- which(e>0)[1]
to find the first element for which e!=0 but now I want to be sure that also all subsequent elements are >0 cos I'm not searching for the first element e>0 but for the first elemtn 'e>0' for which also all subsequent elements in the vector are larger than 0.
Usually search on the internet sooner or later delivers the proper code but this time I wasn't successful. If one of you knows a solution I'd be very thankful.
BR
Why don't you just reverse the vector and find the first 0?
which(rev(e)<=0)[1]