Solved!Problem with repeat command...probably my fault..

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Solved!Problem with repeat command...probably my fault..

Post by Fasasoftware » Thu Apr 12, 2018 9:34 pm

Hi again,
i have a problem with my script... it's a little problematic.... the repeat go in loop forever!!!don't stop when finish the lines in the field...tha i would expet me....

Target:
i need to found some text in a field ...if exist i find the number of items founds....if the result don't give nothing...then answer " Nothing item found....if found something text then backgroundcolor to green the text found....
that's all...

i thank you a lot in advance to everybody,

Best regarsds,
Lestroso :oops:



Code: Select all

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

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!!!!
      if numerolinee = 1 then pass cercatesto  <------this don't work i don't know why!!!!

      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"
    else
        answer "I Have Found " & conta & " Items!"
        
        pass cercatesto
     else
        if the foundtext is not ttrova then
           answer " I don't have found nothing!!"
           pass cercatesto
        end if
        end if
   end repeat
end cercatesto

Last edited by Fasasoftware on Fri Apr 13, 2018 8:25 pm, edited 3 times in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with repeat command...probably my fault..

Post by bogs » Thu Apr 12, 2018 10:13 pm

Well, I see a couple of problems with the code, as well as a few things that could be shortened.

For instance,

Code: Select all

get the number of  lines  in field "result"
put it into numerolinee
could be done as just [put the number of lines of field "result" into numerolinee] or even [put field "result" into numerolinee], but if you already have a field, why bother with the variable at all?
if numerolinee = 1 then answer "okkkkk" <------this don't work i don't know why!!!!
if numerolinee = 1 then pass cercatesto <------this don't work i don't know why!!!!
Also, unless the field has something in it, it is possible your number of lines is going to be 0, but at the beginning of your repeat loop your subtracting 1 from numerolinee. In other words, if it is 0, and you subtract 1, your going to have either negative 1 line or it will still be 0.

I didn't actually test the code, but if I free up some time later I might.
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Problem with repeat command...probably my fault..

Post by Fasasoftware » Thu Apr 12, 2018 10:45 pm

Thanks bogs....

i thank you for your help... it's not simple for me...but i try to understand....

for this:

Code: Select all

[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....

The problem is that go in loop....you say that the

Code: Select all

get the number of  lines  in field "result"
   put it into numerolinee
i take the value at the begining of the script...and take good the lines of numbers of the field....and don't go in negative....

i thank you a lot ....Lestroso :oops:

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with repeat command...probably my fault..

Post by bogs » Fri Apr 13, 2018 5:42 am

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 :wink: (I'm kinda surprised Klaus didn't pop in with one of his famous 3 word lines to do this :mrgreen: )

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 :)
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Problem with repeat command...probably my fault..

Post by Fasasoftware » Fri Apr 13, 2018 3:03 pm

hi bogs...

thanks for the help....

but the script don't work...i try to modify...but nothing works here....

i need to analize all the lines in field "result" in a loop...and find all the sentences if found.an put them in green..if after all analize don't find anything...answer" I have found nothing!"....

Thank a lot Lestroso :(

Code: Select all

local ttrova
local x
local numerolinee
local conta

on mousedown
   put 0 into conta
  ---put the number of  lines  in field "result" into numerolinee  ---one line code
   put  field "trova" into ttrova
  send "cercatesto" to me --give an error if i don't write this!!!
  
  
end  mousedown

on cercatesto
   -----   repeat with x = 1 to numerolinee 
   -----  subtract 1 from numerolinee
   repeat for each line tline in field "result"
   
   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"
   else 
      answer "I Have Found " & conta & " Items!"
      
      pass cercatesto 
   else 
      
      answer " I don't have found nothing!!"
      pass cercatesto
   end if

end repeat
end cercatesto

end cercatesto


bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with repeat command...probably my fault..

Post by bogs » Fri Apr 13, 2018 4:56 pm

Lestroso, did you copy / paste the code I put up there? Cause your code doesn't look like mine at all, and you've introduced some new problems, some old problems.
.. This code, as I said up there, will never work as I believe you intend it to. First of all, 'else' is an end statement, you have an 'else' followed by another 'else'. If/then statements will only ever have one 'else' in them, if at all. You can have many 'else if...' types, but only one 'else'. The 'else' is at the end to catch any condition that doesn't evaluate through your other tests.

Code: Select all

   else 
      answer "I Have Found " & conta & " Items!"
      
      pass cercatesto 
   else 
I think my earlier assumption about placement on the answer was incorrect, so I would move that completely out of the loop and up to the mouseDown handler, OR to a separate handler, like so -

Code: Select all

local ttrova, x, numerolinee, conta

on mousedown
   put 0 into conta
   put  field "trova" into ttrova
  send "cercatesto" to me --give an error if i don't write this!!!
  hndResult
end  mousedown

on cercatesto
   repeat for each line tLine in field "result"
      find ttrova in tLine 
            if the foundtext is ttrova then
      		add 1 to conta
      		select the foundtext
      		set the backgroundColor of selectedtext to "green"
   	  end if
   end repeat
end cercatesto

on hndResult
   if conta = 0 then // nothing was found...
      answer " I don't have found nothing!!"
   else // something was found, answer the result...
      answer "I Have Found " & conta & " Items!"
   end if   
end hndResult
Hope that helps.
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Problem with repeat command...probably my fault..

Post by Fasasoftware » Fri Apr 13, 2018 6:49 pm

Thank you bogs,
you helped me so much....
Last edited by Fasasoftware on Fri Apr 13, 2018 8:24 pm, edited 2 times in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with repeat command...probably my fault..

Post by bogs » Fri Apr 13, 2018 7:26 pm

Dunno, works here.
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Problem with repeat command...probably my fault..

Post by Fasasoftware » Fri Apr 13, 2018 8:24 pm

Thank you bogs,
you helped me so much....thank you to everybody...

Finally i have create this software...it's not perfect but it works well a little...uceptable...
the problem was the Find Command....because it will not stop and it was the problem with th repeat statement....i have solved in part the problem with a send"cercatesto" call...repeated until false...

best regards,
Lestroso :D :D :D

Code: Select all

local ttrova,conta,ciclo

on mousedown
   put 0 into conta  ---put zero to reset the count od items found
   put  field "trova" into ttrova-----put the item to find in memory
   send "cercatesto" to me ---launch the  program
end  mousedown

on cercatesto
------ i have delete the repeat statemant with a call to itself until find nothing...

        find ttrova in field "result"  --------find the item in field "result"
        if the foundtext is ttrova then  -----if the text is found then
         select the foundtext  -----select the text
         set the backgroundColor of selectedtext to "green" ----set the backgrondcolor of text to green
         add 1 to conta---- increment the item found!
         send "cercatesto"  ----here the trick call itself the prog until false
      else
     send"mytest" ---if false send mytest!
        end if
end cercatesto

on mytest
    if conta = 0 then // nothing was found...
            answer " I don't have found nothing!!"
else 
      answer "I Have Found " & conta-1 & " Items!"---i have trimmed with -1 to adjust the number of item ---found

   end if   
end mytest


ClintMA
Posts: 39
Joined: Thu Jan 21, 2016 2:52 am

Re: Solved!Problem with repeat command...probably my fault..

Post by ClintMA » Sat Apr 14, 2018 11:22 am

Lestroso,

Perhaps a small correction for you:

In the "myTest" handler you have this line:
answer " I don't have found nothing!!"
If your actual program is in Italian, and you have just translated the code here for our benefit, then you don't need to do anything. But if your stack displays English prompts, then you should rewrite the answer code using more idomatic English:

Perhaps —

answer "I didn't find anything!"

Or —

answer "I found nothing!"

Or —

answer "Nothing was found!"

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Solved!Problem with repeat command...probably my fault..

Post by Fasasoftware » Sun Apr 15, 2018 4:40 pm

DEAR ClintMA,

I'm sorry for my bad english....i must study hard to improve it...

In any way thanks a lot for your sentence....

best regards,
Lestroso :D :D :D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Solved!Problem with repeat command...probably my fault..

Post by bogs » Sun Apr 15, 2018 7:49 pm

Heya Lestroso,
I finally had time to look at this problem you posed in a bit more depth today. This is what I came up with, see if it works for you.

Code: Select all

on mouseUp
   repeat for each word x in field "result" // scans each word in field result...
      add 1 to tmpNum // add 1 to mark which word in the field we are on...
      if x contains field "ttrova" then set the backColor of word tmpNum of field "result"  to "green"
// I used 'contains' because if the word is followed by a period with
// no space between them, it will still be highlighted...
   end repeat
   if tmpNum > 0 then
// making 2nd use of the variable...
      answer "I Have Found " & tmpNum & " Items!"
   else
      answer "No match found!"
   end if
end mouseUp
Hope that helps you out in some way.
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Solved!Problem with repeat command...probably my fault..

Post by Fasasoftware » Sun Apr 15, 2018 9:14 pm

thanks bogs,

your script is beauty but don't work...i have also added a local x variable....but nothing works here....

i thank you a lot for your time dedicated to me....

best regards,
Lestroso :D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Solved!Problem with repeat command...probably my fault..

Post by bogs » Mon Apr 16, 2018 12:02 am

Heya Lestroso,
I tested it to make sure it worked. I'll upload the stack so you can see for yourself it works as advertised.

However, thank you for making me re-test it, as I did find an error in my own logic. I made a slight change and added a second variable and one line of code (strictly to deal with the answer dialog). The change was -

Code: Select all

on mouseUp
   repeat for each word x in field "result"
      add 1 to tmpNum
      if x contains field "ttrova" then
         set the backColor of word tmpNum of field "result"  to "green"
         add 1 to tmpNum2 // 2nd variable added to actually count words found, doh!
      end if
   end repeat
   
   if tmpNum2 > 0 then
      answer "I Have Found " & tmpNum2 & " Items!"
   else
      answer "No match found!"
   end if
end mouseUp
There are no buttons, click anywhere to start the demo. It should work in Lc 6.5 or higher.
matchAndColorText.zip
Click anywhere on the card, and the demo will start.
(901 Bytes) Downloaded 180 times
*Edit - I would also suggest you not keep using 'contains', but create a better matching either with "is" or "=", because by using contains, if they were to type in "i" for instance, you'd come up with inaccurate results.
Image

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: Solved!Problem with repeat command...probably my fault..

Post by Fasasoftware » Mon Apr 16, 2018 12:41 pm

Dear Bogs!!! You're right!!!

Your script is very good!

I have downloaded your file and tested...it's working perfectly.....

Thank a lot so much!!!

Cheers!

Lestroso 8) 8) 8)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”