Fasasoftware wrote: ↑Thu Apr 12, 2018 10:45 pm
for this: [put the number of lines of field "result" into numerolinee]
....
get the number of lines in field "result" put it into numerolinee this is not the problem...
this line work perfect....
Lestroso, I know that the 2 lines you had would work, I think you missed the point. In this case, we are talking about efficiency and readability, is it more efficient and readable to write 1 line that will do what you need, or two lines? I also would tend not to rely on 'it', even though that works, 'it' can change when you least expect 'it' too
(I'm kinda surprised Klaus didn't pop in with one of his famous 3 word lines to do this
)
On to the problem at hand, I finally had time to track what you wrote, there were a few issues. First, I'll post what I semi cleaned this up to.
Of course, I don't know the full intent of what you are trying for, but I am guessing you are trying to find whatever is in field "trova" that matches in field "result". This code *works*, but is not the greatest way to do it, instead I tried to work with what you provided.
Code: Select all
// converted global to local variables...
local ttrova, x, numerolinee, conta
on mousedown
put 0 into conta
put the number of lines in field "result" into numerolinee
put field "trova" into ttrova
cercatesto
end mousedown
on cercatesto
repeat with x = 1 to numerolinee
subtract 1 from numerolinee -- not sure why you are
-- subtracting from the total, the start of the repeat loop, and I am
-- pretty sure this is the least efficient way to tackle the problem.
find ttrova in field "result"
if the foundtext is ttrova then
add 1 to conta
select the foundtext
set the backgroundColor of selectedtext to "green"
answer "I Have Found " & conta & " Items!"
else
-- the foundtext is not ttrova
answer " I don't have found nothing!!"
end if
end repeat
end cercatesto
Now I'll explain what I took out and why (commented) -
Code: Select all
-- globals are completely unnecessary for this script, and potentially cause consequences
-- if their values change to something you are not expecting in other parts of your program.
-- For this one script, local variables are more than sufficient. Also, when you are declaring
-- variables, it is easier to read when they are on one line, separated by a comma. If you
-- were declaring more than one type of variable, then you would use more than one line.
global ttrova
global x
global numerolinee
global conta
on mousedown
put 0 into conta
get the number of lines in field "result"
put it into numerolinee
put field "trova" into ttrova
send "cercatesto" to me
-- this line calls the handler, no need to "send" anything, so I shortened it to just
-- the handler name without quotes.
end mousedown
on cercatesto
repeat with x = 1 to numerolinee
subtract 1 from numerolinee
if numerolinee = 1 then answer "okkkkk" --this don't work i don't know why!!!!
-- This wasn't working because it was never reached. Set a debug break point at
-- the beginning of this loop, and when it hits step through your code using the F11
-- key. You'll find your loop is exiting in that if/then set, which had its own issues.
if numerolinee = 1 then pass cercatesto --this don't work i don't know why!!!!
-- this lines placement is questionable at best, as soon as your count hits 1 this would
-- exit the loop, but the loop would exit anyway on completion, or on condition when
-- nothing is found, so it is pointless.
find ttrova in field "Result"
-- even though the spelling of 'result' with a capital did not seem to be a problem, I
-- corrected it in my own version. If you make typos, I really recommend sticking
-- with the strict variable checking in preferences.
if the foundtext is ttrova then
add 1 to conta
select the foundtext
set the backgroundColor of selectedtext to "green"
else -- you have an else but it seems like the next line should be here.
answer "I Have Found " & conta & " Items!"
pass cercatesto -- I assume this was not supposed to be here, as this would exit
else -- you have an 'else' here, but the 'if' is on the next line, unpredictable result.
if the foundtext is not ttrova then -- this is completely unnecessary, as the first 'if' condition
-- works on whether the foundtext is ttrova or not. If it is not, the next statement would execute
-- without the condition tested for by default.
answer " I don't have found nothing!!"
pass cercatesto -- this statement again is unnecessary, your loop exits when finished.
end if
end if
end repeat
end cercatesto
I hope this was of some help to you