I was searching through SO and google for a solution to my problem for 2 days but I had no luck. My problem is that I have an array and I initiate its elements with 0\'s, an
The | while read element; do ... done is running in a sub-shell, so its updates to the global are lost when the sub-shell exits.  
One solution is to use bash's 'process substitution' to get the input to the while loop to run in a subshell instead. See: Reading multiple lines in bash without spawning a new subshell?