for $w (0..$#a) How big is a? This could be mighty slow
I'd consider using local instead of duplicating arrays
References aren't free. If you are doing *a lot* of dereferencing it can be a good idea to assign something like $a[$v] to a temporary value outside of the the inner for loop, and referencing $temp[$w] instead.