Help with placing iterated function results into a list

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
ckmcdevittGA6a53
Posts: 4
Joined: Mon Jan 09, 2012 4:47 pm

Help with placing iterated function results into a list

Post by ckmcdevittGA6a53 » Mon Feb 20, 2012 11:52 pm

I was trying to write a simple program to factor numbers. I was writing a function for each number (i.e.

On factorThisNumber
divide thisnumber by 2
if thisnumber is an integer then
put thisnumber & "*2" into field Factors2
end if
multiply thisnumber by 2
End factorThisNumber

...then for 3, 4, etc...

This was getting quite tedious, so I thought I would try my hand at creating an iteration. I know that the math and syntax is correct in my iteration because it works great for 1... but I do not know how to make all the factors go into a single list, or to have them go into different fields. Suggestions?

Thanks in advance.
Carrie

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

Re: Help with placing iterated function results into a list

Post by dunbarx » Tue Feb 21, 2012 1:26 am

Hi,

Do you mean find all the factors of a number? And put those factors into a list? If you have a field "yourField" with a number in it and a button "yourButton", place this into the button script:

Code: Select all

on mouseUp
 factor field "yourField"
end mouseUp

on factor var
   put 2 into counter
   repeat until counter > var
      if (var / counter) is an integer then
         put counter & "," after tList
         divide var by counter
         next repeat
      else add 1 to counter
   end repeat
   answer char 1 to -2 of  ("1," &  tList)
end factor
This gives the factors as a list of items. Do you also need help in placing those factors into some form that you find useful? Write back if so.

Craig Newman

ckmcdevittGA6a53
Posts: 4
Joined: Mon Jan 09, 2012 4:47 pm

Re: Help with placing iterated function results into a list

Post by ckmcdevittGA6a53 » Tue Feb 21, 2012 2:11 am

Thank you! The code is close to what I was planning, and the prime factorization will be helpful in the next stage of what I am doing.

But for now, I was hoping that a person could put a number (like 24) into a field and push a "factor me" button where they would be given a list (1*24, 2*12, 3*8, 4*6) of factors for that number placed into a vertical list format in a field on the screen.

Thanks again for all your help! I look forward to your response!

Carrie

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

Re: Help with placing iterated function results into a list

Post by dunbarx » Tue Feb 21, 2012 2:56 am

Hmmm.

I am going to give you this, but pretty soon you must play around by yourself. Please let me know if the code so far is completely readable for you, that is, you can follow the script step by step. With fields as you can see in the script, place this in a button

Code: Select all

on mouseUp
 factor fld  "yourArgumentField"
end mouseUp

on factor var
   repeat with y = 1 to round(sqrt(var))
      if (var / y) is an integer then
         put var/y & "*" & y & return after tList
      end if
   end repeat
   put  tList into fld "yourAnswerField"
end factor
Let me know...

Craig Newman

ckmcdevittGA6a53
Posts: 4
Joined: Mon Jan 09, 2012 4:47 pm

Re: Help with placing iterated function results into a list

Post by ckmcdevittGA6a53 » Tue Feb 21, 2012 5:48 am

That is actually very similar to the code I had written (I am a math teacher and my mathematics and logic were correct) I was just unaware of the tList feature. I tried using several different list and menu options in the dictionary but the tList is not in the dictionary. Thanks very much for the help.

kdjanz
Posts: 142
Joined: Fri Dec 09, 2011 12:12 pm
Location: Fort Saskatchewan, AB Canada

Re: Help with placing iterated function results into a list

Post by kdjanz » Tue Feb 21, 2012 6:15 am

tList is not a function, so you will not find it in the dictionary. tList is just a variable that Craig is using to capture the factors. t prefix on a variable name is just common livecode naming shorthand to remind the programmer that it is a temporary variable that is local to the function and will not stick around between uses - once the function ends it is erased from memory. The name tList tells everyone that this variable is temporary and is being used to create a list.

Code: Select all

         put var/y & "*" & y & return after tList
This line does the formatting and builds a string in the pretty format that you want to show the user (## * #) then puts a return character after it to end the string. The last part of the line "after tList" actually puts it into the variable for safe keeping until all the factors are found. The "after" is very important to put the new line at the bottom of the list - if you used "into", it would wipe out anything already in the variable, and if you used "before" it would add it to the top of the list.

At the end of the repeat, the tList variable holds all the values, and the line

Code: Select all

   put  tList into fld "yourAnswerField"
puts the whole thing into the field where it becomes visible to the user.

It would have been overkill for this purpose, but tList could have been an array variable or almost any other container. Or Craig could have put each line into the field as it was found, but inserting into fields is much slower than adding to a variable, so he chose to accumulate everything in memory before he dumped it all at once to the field.

Hope this takes you a bit deeper,

Kelly

ckmcdevittGA6a53
Posts: 4
Joined: Mon Jan 09, 2012 4:47 pm

Re: Help with placing iterated function results into a list

Post by ckmcdevittGA6a53 » Wed Feb 22, 2012 1:40 am

Yes! That is very helpful! Thank you!

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

Re: Help with placing iterated function results into a list

Post by dunbarx » Wed Feb 22, 2012 2:54 pm

Kelly has given a very nice mini-tutorial about LiveCode.

I suspected that you were very new to this, and so asked that you examine each line so that you can learn how they worked. Please do so.

That you did not know how to place data in a variable makes me think you might be confused by some of the code lines I wrote, which contain layered elements. The fact that you are a math teacher will likely help you see the intent, now it is time to learn the language. For example, where does "var" come from?

Write back often. This can be fun for everyone.

Craig Newman

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”