changing values in a list

53 views
Skip to first unread message

Chris Harvey

unread,
Aug 4, 2019, 1:56:38 PM8/4/19
to GAMA
Hello,

I have a list, of size 10.  I want to delete the first value and add a new value at position 10 at each timestep.  Once this occurs I sum the values in the list.  So in effect I am updating the content of the list at each timestep.

I am getting an error message 'Index 9 out of bounds of daily' .  I'd like to know why I'm getting the error message and what I need to do to make this work.     Currently, what I have (and doesn't work) is the following:


list <float>daily <- list_with(10,0);

remove first(daily) from: daily;
daily[9] <- (consump);
daily_sum <- sum(daily);



Thank you,

Chris

Brahim Ali Koreimy

unread,
Aug 4, 2019, 9:59:26 PM8/4/19
to gama-p...@googlegroups.com
Hello,
this is normal because you have an item in the list and the list has a size of 9 item from 0 to 8.
so if you want to delete and insert an element in the last position you do this:

ist <float>daily <- list_with(10,0);

remove first(daily) from: daily;
daily[8] <- (consump);
daily_sum <- sum(daily);
if you want to replace the last element without reducing the size there you should not delete go directly:
ist <float>daily <- list_with(10,0);
daily[9] <- (consump);
daily_sum <- sum(daily);

thank you
Brahim Ali Koreimy
0769654828


--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/fac9920e-1a81-473d-badc-60d8208f0270%40googlegroups.com.

Benoit Gaudou

unread,
Aug 4, 2019, 10:15:36 PM8/4/19
to gama-p...@googlegroups.com
Dear all,

Brahim is definitely right on the issue of index.

I just would like to add a comment on his solution. It proposes to remove the first element and to replace the last one of the remaining list.
But this algorithm has a flaw : it reduces the length of the list by 1 at each iteration. (and thus after 10 iterations the list is empty).

If Chris wants to shift the list to the left, she needs to add a new element at the end of list (in order to keep the length).

list<int> daily <- list_with(10,0) accumulate(rnd(10));

write sample(daily);


remove first(daily) from: daily;

write sample(daily);

// 2 writtings are equivalent 

// add rnd(10) to: daily;

daily <- daily + rnd(10);

write sample(daily);


Cheers

Benoit

Chris Harvey

unread,
Aug 4, 2019, 10:19:34 PM8/4/19
to GAMA
Thank you for getting back to me Brahim.

To be clear on what I'm attempting,

list<float>A <- [2.3],[1.2],[6.7];

what I want to do is this:

list<float>A <- [1.2], [6.7], [new value];

so, I deleted the first value and moved all the values up by one place and inserted the new value at the end.

Chris
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

Chris Harvey

unread,
Aug 4, 2019, 10:31:43 PM8/4/19
to GAMA
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

Chris Harvey

unread,
Aug 4, 2019, 10:32:49 PM8/4/19
to GAMA
Hello Benoit,


Yes.  This is exactly what I wanted to do.  Perfect.

Cheers,

Chris




On Sunday, August 4, 2019 at 9:15:36 PM UTC-5, Benoit Gaudou wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

Srirama Bhamidipati

unread,
Aug 5, 2019, 5:53:09 AM8/5/19
to GAMA
Hi Chris,

For any reason if you wish to keep the list (without deleting initial sequences), you can use the last  operator with an extra argument.
In plain English you would read : give me last 5 items from a list  
and in GAML like so: last ( 5, my_list) .

This will keep your list for ever and always return only the last n requested items.


** This argument applies also to the first operator. 

Srirama

Chris Harvey

unread,
Aug 5, 2019, 4:32:12 PM8/5/19
to GAMA
Good Info Srirama! 

I'm using the list to determine how much food my tarpon agent consumed over the last 24 hours.  If the consumption level exceeds a threshold, then the agent is not permitted to consume any more food,otherwise it can consume at the rate defined for it.  So I wanted to always be putting the consumption data for the last timestep into the list and removing the first consumption data value from the list as it would have occurred longer than 24 hours ago.  However, at some point I may want to evaluate the total amount consumed and at that point I can see needing to keep all of the values.


Chris

You received this message because you are subscribed to a topic in the Google Groups "GAMA" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gama-platform/rZ_XJJFNBg4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/0c1e216a-e243-4042-8f5f-4003e06b278c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages