GME

Geospatial Modelling Environment

for (For Loop)

A programming device that facilitates iterative repetition of commands

Description

A for loop is a programming device that allows you to repeat a set of commands many times with only a few lines of code. The format of the for loop in this software is similar to that of R. The word 'for' indicates that you are starting a loop. The next expression controls the number of iterations: for(i in 1:100) implies that the loop is going to repeat itself 100 times. The letter 'i' is the index variable name - it could be any simple string you wish to use (another letter, or even a text string like 'num'). You can use this variable in subsequent commands to construct command arguments using the paste() function.

The set of commands that are to be repeated must be contained by a set of braces { }, and each command must end with a semi-colon to distinguish it from the next command. Nested for loops are supported, but it is essential to use a different index variable for each nested loop (e.g. i, j, k for three nested loops, for instance). For loops can be included within scripts that you call with the 'run' command.

If you want to iterate across a non-continuous range of numbers, or a sequence of arbitrary values, then you can combine the use of a GME variable with a for loop. For example, given a list of animal ID numbers, i.e. uid <- c(12, 14, 19, 21);, a for loop could be specified as for(i in 1:length(uid)) { ... }, and the id numbers can be retrieved from within the loop using uid[i] whereever needed.

An advanced problem is building the for(i in 1:100) statement using dynamic variables. For instance, rather than specify 1 and 100, you might want to use variables you have previously defined (see 'Strategic commands, functions and clauses' section). This is possible, however: you must ensure the variables are defined (and visible using the ls() command) prior to submitting the for loop command text. I.e. you cannot define the variables and use them in a for loop in the same block of text that is submitted to GME. This is a command interpreter limitation that I intend to resolve in a future update. This should not be a limitation to the majority of users.

See the Automation and Batch Processing section near the beginning of the manual for more detailed examples and explanation of how to use for loops.

See also: paste, r.eval, list.vector, list.raster

Syntax

for(i in x:y) { ...commands... };

i in x:yi is the variable name that is incremented within the for loop (it can be any simple string), x is an integer at which the looping begins and y is another integer (greater than or equal to x) at which the looping ends, e.g. for(i in 1:100). The command is followed by a series of other commands enclosed within braces and separated by semicolons that are executed at each iteration of the loop: { ...commands... }

Example

for(i in 1:5) {

genrandompnts(poly="C:\data\fields.shp", sample=100, out=paste("C:\data\samples", i, ".shp"));

addxy(in=paste("C:\data\samples", i, ".shp"), prefix="COORD");

r.plotxy(in=paste("C:\data\samples", i, ".shp", xfield="COORDX", yfield="COORDY");

};

Results in the generation of 5 point datasets, with COORDX, COORDY fields in the attribute table, and five R graphs of the x, y coordinates.


Messages

Please consider making a purchase to support the continued development of these tools  Read more...

Tips on how to use this interface efficiently  Read more...

Links

Open Source GIS

Copyright © 2001-2014 Hawthorne L. Beyer, Ph.D., Spatial Ecology LLC    Connect on LinkedIn