Help with a quick coding issue

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

Post Reply
stitch4
Posts: 1
Joined: Fri Apr 17, 2015 6:16 am

Help with a quick coding issue

Post by stitch4 » Fri Apr 17, 2015 6:25 am

So I'm writing a cute little app for fun and I'm running into a problem with my coding. I'm writing a custom handler that is meant to put the colornames(all colors in livecode) into a text box, and make them clickable to show up in a small graphic on the card. I can get it working, but I'm trying to create a preopenstack handler that will put all of the color names into the text field, but not keep the ones that have numbers next to them e.g. black, blue, etc., but getting rid of black1, blue2, etc. This is my handler so far :

on (custom handler)

get the colornames

put it into fld "colors"

repeat with j = 1 to the number of lines of fld "colors"

if line j of fld "colors" contains a number then

delete line j of fld "colors"

end if

end repeat

end (custom handler)

Could really use some help on this. A fix for my code, and maybe a quick explanation as to why it's not working. Please and Thank You.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Help with a quick coding issue

Post by Dixie » Fri Apr 17, 2015 8:15 am

Stitch...

this works...:-)

Code: Select all

on cleanColourNames
   put "1,2,3,4,5,6,7,8,9,0" into numberList
   get the colornames 
   put it into fld "colors"

   repeat with j =  the number of lines of fld "colors" down to 1
      if the last char of line j of fld "colors" is among the chars of numberList then
         delete line j of fld "colors"
      end if
   end repeat
end cleanColourNames
or as SparkOut pointed out 'using for each' is a whole lot faster...

Code: Select all

on cleanColourNames
   put "1,2,3,4,5,6,7,8,9,0" into numberList
   put the colornames into colourVariable
   
   repeat for each line thisLine in colourVariable
      if the last char of thisLine is not among the chars of numberList then
         put thisLine & cr after theCleanColours
      end if
   end repeat
   
   delete the last char of theCleanColours
   put theCleanColours into fld 1
end cleanColourNames
Last edited by Dixie on Fri Apr 17, 2015 8:40 am, edited 1 time in total.

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Help with a quick coding issue

Post by SparkOut » Fri Apr 17, 2015 8:25 am

A few things:

Iterating through a field is hugely more resource hungry than a variable. Put the field contents into a variable, work on that, and then update the field at the end.

"repeat for each line tLine in tVar" is hugely more efficient than repeating with an index value and counting the lines. If tLine does not contain a number then add that line to a new variable and build up a list of valid results, then put the new variable into the result field. This would eliminate your current problem which is:

When using a line count, any operation you make which affects the list will have an impact on the counter.
Say line 7 is to be deleted, and so will line 8. When you delete line 7 though, line 8 is not the same as it used to be - that line is now line 7. There is not an empty line 7. So when your next check comes, it will look for line 8, which is the one after the one that has slipped past into line 7.

So, when you do need to iterate over a list with a line counter that affects the indexing like this, the way around the problem is by "repeat with j = the number of lines of tList down to 1"
That way it doesn't matter if when deleting line 7, line 8 became the new line 7 because your next line to check will be 6 and the adjusted lines will all be after the current index.

amelia16
Posts: 3
Joined: Sat Apr 21, 2018 10:07 pm

Re: Help with a quick coding issue

Post by amelia16 » Sat Apr 21, 2018 10:15 pm

I'm also doing an application similar to this one! I have a list of the colors on a field and I want the user to click on a color and click on a section of the house, like the chimney or the door (each section has a graphic outlining it) and I want the background color to change to the color they chose but I have not the slightest clue how to do that! Please help!

This is my handler so far:

on mouseUp
repeat for each line
set the color of me to the backgroundColor of the target
end repeat
end mouseUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Help with a quick coding issue

Post by dunbarx » Sun Apr 22, 2018 3:35 pm

You have syntax issues.

Make a locked field (named "colorList") with some color names and a couple of buttons on a new card. Put this in the card script:

Code: Select all

on mouseUp
   if "colorList" is in the name of the target then
      set the currentColor of this cd to the clickText
   end if

   if "button" is in the name of the   target then
      set the backColor of the target to the currentColor of this cd
   end if
end mouseUp
Click on any line in the field. Click on any button. Do you see what is going on? Write back whether you do or not.

Craig newman
Last edited by dunbarx on Mon Apr 23, 2018 6:01 pm, edited 1 time in total.

amelia16
Posts: 3
Joined: Sat Apr 21, 2018 10:07 pm

Re: Help with a quick coding issue

Post by amelia16 » Mon Apr 23, 2018 5:02 pm

When I click on a color and then a button, the background color of the button changes to the color I selected. I switched "button" to "graphic" and it works! Thanks so much!!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”