LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Display Array of Strings with White Spaces (https://www.linuxquestions.org/questions/linux-newbie-8/display-array-of-strings-with-white-spaces-835677/)

coffee arabia 10-01-2010 01:30 PM

Display Array of Strings with White Spaces
 
Hello, this is a C shell question.

I generated an array of strings, but each string has white spaces between tokens. My script is

set cmd1 = 'apple orange'
set cmd2 = 'tea coffee'
set cmd_list = ($cmd1 $cmd2)

The problem is when I tried to display each element from that array

foreach cmd ($cmd_list)
echo "cmd_list is $cmd"
end

What I want to see is

cmd_list is apple orange
cmd_list is tea coffee

but instead I saw

cmd_list is apple
cmd_list is orange
cmd_list is tea
cmd_list is coffee

How should I write the foreach command to fix the problem?

Thanks

colucix 10-01-2010 02:27 PM

You can try the :q modifier to prevent word splitting. For example:
Code:

#!/bin/tcsh
set cmd1 = "apple orange"
set cmd2 = "tea coffee"
set cmd_list = ($cmd1:q $cmd2:q)

foreach cmd ($cmd_list:q)
  echo "cmd_list is $cmd"
end

should do the trick. Please, note that this cannot be called array (as far as I know the C-shell does not manage arrays) but a list. Hope this helps.

Ah... and welcome to LinuxQuestions! :)

coffee arabia 10-01-2010 04:24 PM

Thanks!
 
It works!

grail 10-02-2010 12:48 AM

Please mark as SOLVED if you have your solution.


All times are GMT -5. The time now is 03:05 AM.