Array; while read line do problem

Hi - I don't understand why the following script isn't working. I want to read the contents of a while loop into an array, but it looks like the array is destroyed once the while loop is finished. Can anybody help?

someFile:

PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

Script:

cat someFile |
        while read l; do
                echo "array[${#array
[*]}]=\"$l\""
                array[${#array
[*]}]="$l"
        done

for ((i=0;i<${#array
[*]};i++)); do
        echo ${array}
done

Output:

array[0]="PC_1 wf_test1 Test"
array[1]="PC_2 wf_test2 Test"
array[2]="PC_3 wf_test3 Test"

Desired Output:

array[0]="PC_1 wf_test1 Test"
array[1]="PC_2 wf_test2 Test"
array[2]="PC_3 wf_test3 Test"
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

I'm not at a machine to run your code...but, where within the while loop are you setting and incrementing the array subscript?

COUNT=1
cat someFile | while read l; do
       echo "array[$COUNT]=\"$l\""
       array[${COUNT}]="$l"
       ((COUNT += 1))
done

Your problem is the piped while read starts a sub shell:

Have a look at this wiki page on the issue.

#!/bin/bash
while read l; do
    echo "array[${#array
[*]}]=\"$l\""
    array[${#array
[*]}]="$l"
done < someFile
for ((i=0;i<${#array
[*]};i++)); do
        echo ${array}
done

@Chubler XL - u r right on. Thanks for that link -- very handy. I've had problems w/ while loops before and never knew what the deal was.

@purdym - no need for the extra increment since arrays are zero-based index.
array[${#array
[*]}]="$l"