首页 > 解决方案 > tcsh array variable substitution

问题描述

I'm attempting to modify a script that works for a different application. i could start over, but I'd like to learn how to do this. Here are some arrays I'm manually setting instead of using user input or an input file:

set mbeg= (1 2 3 4)
set mmid= (5 6 7 8)
set mend= (9 0 0 0)

what I really want to do is loop through $time and $case and have it return the array value for the loop variables.

foreach time (beg mid end)
   foreach case (1 2 3 4)
      echo \${m$time}[$case]

For this example, I want it to return: 1 2 3 4 5 6 7 8 9 0 0 0 0

I can get as far as getting echo \${m$time} to return "mbeg" but i can't figure out how to get the array counter in there. Is this something simple that can be done, or should I change the strategy? Other strategies include brute force input of the array name and if/else statements to choose only the array corresponding to the $time variable.

foreach time (beg mid end)
foreach j ( 1 2 3 4 )
   if ($time == beg) then
      echo $mbeg[$j]
   elseif ( $time == mid ) then
      echo $mmid[$j]
   else
      echo $mend[$j]

I could also attempt to read from an input file. To date, I have done this using grep and sed to reduce the file to a single variable. Thanks.

标签: arraysvariablestcsh

解决方案


推荐阅读