Looping through multiple arrays

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

abielr
Posts: 9
Joined: Wed Feb 04, 2009 12:25 pm

Looping through multiple arrays

Postby abielr » Wed May 27, 2009 5:26 am

I'm trying to loop through a group of series that I use to create graphs. For each graph I need the series name, a name for the graph object, and a string to use as the graph title. Example code is below.

Code: Select all

for %s %o %c regional::casatxa atlanta "Atlanta" regional::casboxa boston "Boston"
   graph gr{%o}.line @pcy({%s})
   gr{%o}.addtext(t) {%c}
next


My concern with this code is that my first line is going to get very long and unwieldy if I have dozens of series to loop through. Moreover, what if I later need to loop through just the series names? In this example they are not stored in a separate array, so I would have to retype them.

Is there a better approach? It seems ideal to be able to store each concept in its own array, and then loop through the arrays in parallel. Unfortunately I do not see any way of doing this in the documentation.

Thank you.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13319
Joined: Tue Sep 16, 2008 5:38 pm

Re: Looping through multiple arrays

Postby EViews Gareth » Wed May 27, 2009 8:03 am

Best thing to do is to put the series into a group. Then loop through the number of series in the group, and grab the series name from the group one at a time:

Code: Select all


group g x y z

for !i=1 to g.@count
     %name = g.@seriesname(!i)
    'do stuff here

next
Follow us on Twitter @IHSEViews

abielr
Posts: 9
Joined: Wed Feb 04, 2009 12:25 pm

Re: Looping through multiple arrays

Postby abielr » Wed May 27, 2009 8:55 am

I see how the group solution is useful for pre-existing series. However, in my example I have (a) pre-existing series (those items in the database), (b) strings not enclosed in quotes which are meant to be part of an object name, and (c) pure strings which are used as a title. I can imagine using a whole separate loop with (b) to build the series and add them to a group ahead of time, and another loop to create a table holding the titles (c), but at this stage there are so many extra loops that it isn't worth it.

Any thoughts?

Thanks very much.


Return to “Programming”

Who is online

Users browsing this forum: Google [Bot] and 36 guests