3

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.

2
  • The c tag is for C programming language related questions. csh while named the "C Shell" has nothing to do with the C programming language. I removed that tag for you. Commented Sep 14, 2010 at 19:04
  • I must've typed C and shell as separate tags, my mistake. Commented Sep 14, 2010 at 19:06

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.