7.3 Foreach loopsForeach loops may be used to run a script block once for each item in a list or dictionary. Alternatively, if a string is supplied, it is treated as a filename wildcard, and all matching files are returned. For example: foreach x in [-1,pi,10] { print x ; } foreach x in "*.dat" { print x ; } myDict = { 'a':1 , 'b':2 } foreach x in myDict { print x ; } The first of these loops would iterate three times, with the variable x holding the values !ls *.dat An error is returned if there are no files in the present directory which match the supplied wildcard. The following example would produce plots of all of the data files in the current directory with filenames foo_*.dat or bar_*.dat as eps files with matching filenames: set terminal eps foreach x in "foo_*.dat" "bar_*.dat" { outfilename = x outfilename =~ s/dat/eps/ set output outfilename plot x using 1:2 } If a dictionary is supplied to loop over, then the loop variable iterates over each of the keys in the dictionary.
|