views:

61

answers:

2

I have a Cshell script that I am modifying to have related input and output locations. the functionality all happens in a foreach loop like so:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
foreach location ($INPUT_LOCATION_LIST)
#***Do some stuff ***
end

I would like to have an output list with different values than the input list but traverse through it each iteration through the foreach loop. The man for foreach simply has

foreach name (wordlist)

as the definition. so only dealing with a single one. my current thought on dealing with it is have the wordlist contain both input and output location and then parse it out in the script:

set INPUT_LOCATION_LIST = "loc1;out1 loc2;out2 loc3;out3 loc4;out4"

so im wondering if anyone has a better way to do that.

+1  A: 

You can iterate through one list using foreach and through the other one by treating at like an array and using shift:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
set OUT_LIST = (out1 out2 out3 out4)

foreach location ($INPUT_LOCATION_LIST)
    do_something $location $OUT_LIST[1]
    shift OUT_LIST
end
Dennis Williamson
+1  A: 

I don't normally use csh, but your question caught my eye. There's probably a solution with less steps, but this kind of thing worked in my version of csh:

foreach location ($INPUT_LOCATION_LIST)
    set one_word_with_space = ${location:s/;/ /}
    set loc_out = ($one_word_with_space)
    set loc = ${loc_out[1]}
    set out = ${loc_out[2]}
    ...
end

Basic idea is just to change the semi-colon separated string into a space-separated string, then parse that into an array.

Tony