1

In TCSH :

I'm giving two lists containing different files in it. Then I have to pass those list as an array element and then the loop should execute twice as there are only 2 lists. But in this case loop is executing as much time as those files in both the lists.

set list_one = (one.s two.s three.s) set list_two = (four.s five.s) set arr=($list_one $list_two) foreach i ($arr) cat $i > $output.s end 

This is an example of my code, according to me loop should execute only twice(for list_one and list_two), but it's executing five times (for one.s two.s three.s four.s five.s)

    1 Answer 1

    2

    The assignment set arr=($list_one $list_two) flattens the lists into one.

    I'm not exactly sure what you're looking for here, but you could loop over the names of the lists with

    set names=(list_one list_two) foreach i ($names) echo $i end 

    But getting from there to actually using the first two lists after you have their name in a variable seems trickier. I can't find if tcsh supports indirect variable references, other than via eval. Something like this seems to work, but getting the quoting right on the eval seems fickly:

    #!/usr/bin/tcsh set l1 = (aa bb cc) set l2 = (dd ee) set names = (l1 l2) foreach i ($names) eval set x = \(\$$i\) foreach j ($x) echo $i $j end end 

    But it values containing whitespace get split, and I don't have an idea how to fix that.


    Honestly, I'd switch away from Tcsh, and do the same in e.g. Zsh :

    l1=(aa bb cc) l2=(cc dd) names=(l1 l2) for i in $names; do for j in ${(P)i}; do echo $i $j done done 

    That should deal with whitespace in the values, but does drop empty elements.

    1
    • To handle list values with spaces in the tcsh version,  change to eval set x = \(\$$i\:q\) and foreach j ($x:q).CommentedApr 16, 2021 at 8:44

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.