I am working on some sort of quiz and have a list of questions and answers, that get transferred to my view class via the controller. People can ask and answer questions on a page, my system then goes and "collects" those to make a quiz out of them.
If you are the first person to start the program / quiz, the question-list is empty. Therefore I want to check for an empty quiz with an if / else clause, the if-case seems to work fine, but the else-case throws an IndexOutOfBoundsException and I dont understand why. I would think that the else-part will not be used when the question-list is empty, therefore the exception should not be thrown. Should....
View class:
#(questionList: List[Question], answerList: List[Answer], answerRadioForm: Form[Answer])
#if(questionList.length == 0){
No questions yet!
}
else {
<!-- As only the highest ranked question gets put into the List, there is only one entry on first place -->
<b>#questionList.get(0).questionText</b>
#for(question <- questionList) {
#question.questionText - #question.ownerID <br>
}
}
Error:
[IndexOutOfBoundsException: Index: 0, Size: 0]
49 <b>"""),_display_(/*27.8*/questionList/*27.20*/.get(0).questionText),format.raw/*27.40*/("""</b>
So, what am I missing here?
I have found a solution and altough it's bad practice to answer your own question, I have searched for several hours for this and maybe my answer helps somebody else:
There must not be a return / newline between the if / else.
Does NOT work:
#if(questionList.length == 0){
No questions yet!
}
else { ...
Works:
#if(questionList.length == 0){
No questions yet!
} else {
EDIT: As #if(questionList.length > 0){ does work also, is stable against accidentically inserting newlines and is a bit easier to read and understand, I will use this instead of the else.
First, does your code compile, as there is no get method on List. You can use list.headOption instead.
Well, I could use questionList(0)
Another solution.
#questionList.headOption.map(q => <b>{q.text}</b>).getOrElse("No questions yet!")
#for(question <- questionList) {
#question.text - #question.ownerId <br>
}
Related
Route::get('/product',function(){ $product = Product::all(); $filtered_product = $product->reject(function ($product) { $specific_product = $product->where("price",'=',"10.00")->get(); foreach($specific_product as $sp){ return $sp->price; } }); dd($filtered_product); });
I want to exlude some records which match the condition above. I know I can do it in simpler way, but I have a weird habit which I like to do thing in more complex way, So I can be proud of myself.. sound crazy right?...anyway..why the code above return an empty array??? please don't tell me to read the document. I am here because I have read it a thousand time and I still don't get it. thanks
I expect the result which does not include the records which have price 10.00
I added my scraping code below, I would in theory, start on a page with 25 values for me to pull, and appends the way I want it too.
Some pages have less than 25 and gives me error's and blank lines on my .txt. Can smarter minds help me clean this up?
Here is the scraping code for me to use for another area that is working fine.
F3::Loop_Scrape()
Loop_Scrape() {
If ErrorLevel
return
else
prop_1=,prop_2=,prop_3=,prop_4=,prop_5=,prop_6=,prop_7=,prop_8=,prop_9=,prop_10=,prop_11=,prop_12=,prop_13=,prop_14=,prop_15=,prop_16=,prop_17=,prop_18=,prop_19=,prop_20=,prop_21=,prop_22=,prop_23=,prop_24=,prop_25=
Sleep,150
IfWinExist,ahk_class IEFrame
{
pwb:=WBGet()
WinActivate
}
Sleep,350
prop_1:=pwb.document.GetElementsByTagName("TD")[1].innerText
prop_2:=pwb.document.GetElementsByTagName("TD")[12].innerText
prop_3:=pwb.document.GetElementsByTagName("TD")[23].innerText
prop_4:=pwb.document.GetElementsByTagName("TD")[34].innerText
prop_5:=pwb.document.GetElementsByTagName("TD")[45].innerText
prop_6:=pwb.document.GetElementsByTagName("TD")[56].innerText
prop_7:=pwb.document.GetElementsByTagName("TD")[67].innerText
prop_8:=pwb.document.GetElementsByTagName("TD")[78].innerText
prop_9:=pwb.document.GetElementsByTagName("TD")[89].innerText
prop_10:=pwb.document.GetElementsByTagName("TD")[100].innerText
prop_11:=pwb.document.GetElementsByTagName("TD")[111].innerText
prop_12:=pwb.document.GetElementsByTagName("TD")[122].innerText
prop_13:=pwb.document.GetElementsByTagName("TD")[133].innerText
prop_14:=pwb.document.GetElementsByTagName("TD")[144].innerText
prop_15:=pwb.document.GetElementsByTagName("TD")[155].innerText
prop_16:=pwb.document.GetElementsByTagName("TD")[166].innerText
prop_17:=pwb.document.GetElementsByTagName("TD")[177].innerText
prop_18:=pwb.document.GetElementsByTagName("TD")[188].innerText
prop_19:=pwb.document.GetElementsByTagName("TD")[199].innerText
prop_20:=pwb.document.GetElementsByTagName("TD")[210].innerText
prop_21:=pwb.document.GetElementsByTagName("TD")[221].innerText
prop_22:=pwb.document.GetElementsByTagName("TD")[232].innerText
prop_23:=pwb.document.GetElementsByTagName("TD")[243].innerText
prop_24:=pwb.document.GetElementsByTagName("TD")[254].innerText
prop_25:=pwb.document.GetElementsByTagName("TD")[265].innerText
Sleep,350
FileAppend,%prop_1%`n%prop_2%`n%prop_3%`n%prop_4%`n%prop_5%`n%prop_6%`n%prop_7%`n%prop_8%`n%prop_9%`n%prop_10%`n%prop_11%`n%prop_12%`n%prop_13%`n%prop_14%`n%prop_15%`n%prop_16%`n%prop_17%`n%prop_18%`n%prop_19%`n%prop_20%`n%prop_21%`n%prop_22%`n%prop_23%`n%prop_24%`n%prop_25%`n,Docs/MyFile2.txt
return
}
dude you should try creating a dict
here's my python version of what you should do hope it helps
props = {}#Create dictionary
index = 1 #index of the elements
for i in range(1,26): #goes from 1-25
#getting the element and put it in the dictionary
props['prop'+str(i)] = pwb.document.GetElementsByTagName("TD")[index].innerText
#incrementing the index
index +=11
for key, value in props.iteritems():
if value != "":
file.append(value)
sorry i dont know c++ i just saw your question but if you get the idea you can make it
When writing test code, I do a lot of this
if (!cond) {
t.Fatal("error message")
}
It's a bit tedious. So I'd like to achieve the following
CHECK(cond, "error message")
So I attempted this
func CHECK(t *testing.T, cond bool, fmt string, a ...interface{}) {
if !cond {
t.Fatal(fmt, a)
}
}
If it were a C macro it would've worked perfectly. But in Go, the line number where the failure is is wrong.
Is there a fix for this?
Sadly you can't do that.
A workaround would be to get the line / function yourself, something like the trace function from https://stackoverflow.com/a/25954534/145587.
You could possibly make use of runtime.Callers()+runtime.Caller(): the first one gives you the call stack while the second allows to extract the debug info about any arbitrary stack frame (obtained from that list).
Your CHECK() function is always one function call down the place the check should have happened at if it was a macro, so you can inspect the stack frame just above.
Update: the only functon which is really needed is runtime.Caller(). Here's your case, simplified:
package main
import (
"runtime"
"testing"
)
func CHECK(t *testing.T, cond bool) {
if !cond {
_, fname, lineno, ok := runtime.Caller(1)
if !ok {
fname, lineno = "<UNKNOWN>", -1
}
t.Fatalf("FAIL: %s:%d", fname, lineno)
}
}
func TestFoo(t *testing.T) {
CHECK(t, 12 == 13)
}
When saved as check_test.go and run via go test, it produces:
$ go test
--- FAIL: TestFoo (0.00 seconds)
check_test.go:14: FAIL: /home/kostix/devel/go/src/check/check_test.go:19
FAIL
exit status 1
FAIL check 0.001s
where line 19 is the line a call to CHECK() is located inside TestFoo().
While the above answer to use CHECK() function will work, I think that the actual answer is code readibility. Much of Go has been designed as a compromise to increase readibility among the community as a whole. See gofmt for example. Most people will agree that it's format is not best for every case. But having a convention agreed to by all is a huge plus for Go. The same answer is to your question. Go is for writing code for your peers, not for yourself. So don't think "I prefer this." Think "what will people reading my code understand."
Your original code should be like this, without parenthesis.
if !cond {
t.Fatal("error message")
}
This is idiomatic and every Go coder will recognize it instantly. That is the point.
I have a long string similar to this:
"tag1, tag2, tag3, tag4"
Now in my play template I would like to create a foreach loop like this:
#posts.foreach { post =>
#for(tag <- #post.tags.split(",")) {
<span>#tag</span>
}
}
With this, I'm getting this error: ')' expected but '}' found.
I switched ) for a } & it just throws back more errors.
How would I do this in Play! using Scala?
Thx in advance
With the help of #Xyzk, here's the answer: stackoverflow.com/questions/13860227/split-string-assignment
Posting this because the answer marked correct isn't necessarily true, as pointed out in my comment. There are only two things wrong with the original code. One, the foreach returns Unit, so it has no output. The code should actually run, but nothing would get printed to the page. Two, you don't need the magic # symbol within #for(...).
This will work:
#for(post <- posts)
#for(tag <- post.tags.split(",")) {
<span>#tag</span>
}
}
There is in fact nothing wrong with using other functions in play templates.
This should be the problem
#for(tag <- post.tags.split(",")) {
<span>#tag</span>
}
I've a UITextField say txtTitle. I want to check for not blank of that field at the time of inserting data into database.
For that I written
if(![txtTitle.text isEqualToString:#""])
{
//Save
}
But where I am shocked is its not working! I did these type of checking before and its working properly. But not with this case. So that I checking it using following,
if(txtTitle.text!=NULL)
{
//Save
}
It's working properly.
Now here I am confusing about this. I used to print NSLog(#"%#",txtTitle.text) without inputting anything into it. Its printed (null).
Someone please justify the difference between two IF conditions.
Thanks
Maybe you can check for the length property of the string instead, using
if([txtTitle.text length] > 0){
// Save
}
I think the difference is between a completely uninitialized string and a string that has been initialized, but is simply empty.
Hope this helps
#Hemang
As you have mentioned that NSLog gives you (null)..you have to compare like
[txtTitle.text isEqualToString:#"(null)"]
other wise use
if([txtTitle.text length] > 0)
{
}