Page 1 of 1

Iterate over map?

Posted: 13 Sep 2017 03:01
by Bluscre
I might be stupid or too tired right now, but what was the way to iterate over a map again since for(key, value in map) doesn't work?

Code: Select all

ch.gridvision.ppam.androidautomagic.simplelang.a.d: Expression must return a collection but returned {title=com.google.android.apps.youtube.gaming:id/title, views=com.google.android.apps.youtube.gaming:id/num_views, description=com.google.android.apps.youtube.gaming:id/description, date=com.google.android.apps.youtube.gaming:id/upload_date, author=com.google.android.apps.youtube.gaming:id/channel_title, subscribers=com.google.android.apps.youtube.gaming:id/channel_subscribers} (Expression: for (k in el) {
  k = el[k]
}

Re: Iterate over map?

Posted: 13 Sep 2017 07:36
by Desmanto
What do you wanna do? You can use getMapKeys() and getMapValues() to get the keys and values to list.

Code: Select all

el = newMapFromValues(
"title","com.google.android.apps.youtube.gaming:id/title",
"view","com.google.android.apps.youtube.gaming:id/num_views", "description","com.google.android.apps.youtube.gaming:id/description", "date","com.google.android.apps.youtube.gaming:id/upload_date", "author","com.google.android.apps.youtube.gaming:id/channel_title", "subscribers","com.google.android.apps.youtube.gaming:id/channel_subscribers");

x = getMapKeys(el); // this is all the keys
y = getMapValues(el); // this is all the values 

for(i in getMapKeys(el))  //loop thru the Keys
  //do whatever to the key

Re: Iterate over map?

Posted: 13 Sep 2017 20:06
by Bluscre
So like

Code: Select all

for (k in getMapKeys(el)) {
  eval("global_"+k+" = "+el[k]);
}
?

Re: Iterate over map?

Posted: 14 Sep 2017 03:19
by Desmanto
You want to create dynamic glovar from the map? If so, use setValue()

Code: Select all

for(k in getMapKeys(el))
  setValue("global_youtube_{k}", el[k]);
I add "youtube", so those created glovar will be sorted in group.

More about dynamic GloVar
http://automagic4android.com/forum/view ... f=5&t=6813