Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I did in Zsh:

array={geometry, analysis, topology, graph theory, calculus}
echo $array

and then I wanted to add word "math:" to each element like" math:calculus":

while (( i++ < 10)); { echo math:$array[i] }

But it does not work? Why?

share|improve this question
    
um, are you sure this is ZSH? That doesn't look like the correct list initialization syntax to me... –  jj33 Jun 15 '09 at 19:19

4 Answers 4

Works fine for me in zsh with the assignment changed from:

array={geometry, analysis, topology, graph theory, calculus}

to

array=(geometry, analysis, topology, graph theory, calculus)

But zsh has tons of options that change its behavior. Maybe the output 'setopt' might help.

share|improve this answer
    
Also , make sure you reinitialize i to 0 each time you run it –  Kyle Brandt Jun 15 '09 at 19:38

Welp, I'm going to go out on a limb here (because I don't accept that the supporting code is correct) and say that "echo math:$array[i]" is missing a dollar sign and should be "echo math:$array[$i]"

share|improve this answer
    
With mine the i doesn't need the dollar sign. –  Kyle Brandt Jun 15 '09 at 19:41
    
well, at least I was right about the initialization being wonky. The ZSH example I looked at used the dollar sigil when referencing the loop variable. –  jj33 Jun 15 '09 at 19:44

Iterating through an array works better with for because you won't over run the end like your code will (unless you set your limit to the size of the array with ${#array[*]}).

Also, I assume you don't want the commas to be included as part of the strings and you should use parentheses instead of curly braces for your array.

array=(geometry analysis topology "graph theory" calculus)
for i in $array; do echo math:$i; done
share|improve this answer

Just do:

array=(geometry analysis topology "graph theory" calculus)
print -l math:${^array}

or check RC_EXPAND_PARAM for the ${^var} form.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.