Help on Dataset

Hello everybody,
I need help on working with dataset, I have a dataset which consist one of the column as Index to which I want to pass alternatingly value 1 ( each row in Index column would be 0,1,0,1,0,1…) so when ever user makes a new entry it will automatically assign value either 0 or 1
Intention is I will be using a template in a template repeater and each template text will either be blue or white depending on 0 or 1 pass through Index Column template parameter.

I have tried following script but i only updates last index as shown in snapshot, I doubt that issue is with system,dataset.toDataset instruction, its not getting executed for each value of (i). I am a newbie in python programming and just taking baby steps.

Note : pls ignore Value 2 in last dataset entry it was old snap latest is 1 as sen in scripting snap

Dataset


Output console

Thanks :slightly_smiling_face:

To alternate between 0 and 1 for an incrementing value, use modulus.

for i in range(10):
    print i % 2

Index column doesn’t contain any value other than what i intend to pass, I need to run through each row of Index column and set value in Index column

Try:

ds = event.source.parent.getComponent('Template Repeater').templateParams
ds_new = system.dataset.sort(ds, 1)

for i in range(ds_new.rowCount):
    ds_new = system.dataset.setValue(ds_new, i, 'Index', i % 2)
   
event.source.parent.getComponent('Template Repeater').templateParams = ds_new

Ps. you should always update your GUI after you’ve finished changing values to avoid constant updates

2 Likes

Thanks nminchin, that worked :smiley: how did 1 % 2 worked ?

The modulus takes the remainder of the number on the left integer divided by the number on the right.

1/2 = 0 remainder 1  so 1 % 2 = 1

in this case %2 return 0 on even and 1 on odd numbers

I mean, in my code it was updating 1 only in last dataset entry so i was suspecting query was executing only during last (i) entry
I am aware of modulus function but as mentioned un system.dataset.setdataset script, I was thinking i need to execute though alternate (i) dataset entry and pass 1 as stated in Argument example.

ah break makes you leave the while loop.
you should have used continue instead

Because in your code you were never updating ‘data’ in the while loop, so you were always using the original dataset to set the next value and then writing that over GUI dataset. So the only update you saw was the last.

2 Likes