Вопрос

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.

Другие советы

You probably need a list of this form:

ls =: 1 dir '*.*'

which is a list of files not a list of characters like 0 dir '*.*'.

You can use each on this list, e.g.:

]ls =: 1 dir '*'
┌──────────┬──────────────┬──────────┬───────────┬───────┐
│build_defs│build_jconsole│build_libj│build_tsdll│jconfig│
└──────────┴──────────────┴──────────┴───────────┴───────┘

;# each ls  NB. number of characters for each filename
10 14 10 11 7
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top