I've read a few sample codes but I just don't understand how to use the foreach-like loop to iterate through the file collection. Here is how I get the list of files:
ls=:0 dir '*.*'
I can echo this out and it works fine. But what I need to do is process each file 1 at a time using some kind of loop. so something like:
ls=:0 dir '*.*'
foreach (file in ls) {
do something(file)
do something else.. so on
}
in the manuals a for each loop would look like this:
smoutput each i.10
Clearly that's no use, it prints 1 to 10, I don't know how to modify this to work with my directory listing.
The each
keyword is for a single-line loop. There is a for
loop, which you can use inside a defined function (or verb).
Example:
myFunction =: 3 : 0
ls =. 1 dir '*.*'
for_file. ls do.
doSomething ;file
doSomethingElse ;file
end.
)
The for_.
variant is arguably the most useful. The name of your variable file
goes right between the underscore and the period. The important thing to remember is that the for loop will give each item as it appears in the array, so if ls
is an array of boxed strings, each file
will show up as a single boxed string.