Adding object names to a list rather than just their values

raehs555 shared this question 1 month ago
Answered

Hi Guys,

I'm currently using a script to create a set of polygons (the number of polygons changes each time the script is executed). The area of these polygons can then be adjusted using a pre-existing slider. As a later part of the script, I wanted to add the polygon objects (poly1, poly2 etc.) to a list so that I could quickly calculate the sum of their areas. However, if I use SetValue and Append while creating the list of polygons, it seems to append only the value of the area without actually linking the object's information (ie when I adjust the slider which changes the polygons' area later on, the values in the polygon list don't change).

Execute(Sequence("SetValue(areaList, Append(areaList, poly" + i + "))", i, 1, s))

Is it possible to use scripting to create the list, areaList={poly1, poly2,...} rather than a static list of their areas? Thanks!

Best Answer
photo

The whole point of SetValue is to NOT get a dependence, so you don't want to use it here. Just construct the proper string for the list you want, e.g.:

Execute({"areaList = {"+Sum(Sequence("poly" + (i) +if(i<s,",",""), i, 1, s))+"}"})

Comments (4)

photo
1

try naming the polygons A1,A2,A3,A4 and defining the list {A1,A2,A3,A4}

photo
1

Thanks for the reply. The name that I use for the polygons in the script doesn't seem to make a difference. If I manually set areaList = {poly1, poly2...} using the algebra view input I get a list of area values that dynamically update when I change the slider that alters their areas. However, if I use

SetValue(areaList, {poly1, poly2})

either manually or in a script as shown in my last post, the values in the list are static and do not update with the slider. I was hoping there was an alternative to SetValue that would work like areaList = {poly1, poly2...} in a script.

photo
2

The whole point of SetValue is to NOT get a dependence, so you don't want to use it here. Just construct the proper string for the list you want, e.g.:

Execute({"areaList = {"+Sum(Sequence("poly" + (i) +if(i<s,",",""), i, 1, s))+"}"})

photo
1

Thanks artydent for the advice about SetValue, and for the solution. It works beautifully. :)

photo
© 2021 International GeoGebra Institute