in reply to what on earth is going on?

Assuming $var1, $var2, and $var3 are all different, then this:
if(($count != $var1) || ($count !=$var2) || ($count !=$var3))
will always be true.

Your call to system("chosespectraldata.pl $spectralfile $var1 $var2 $var3 > $diroutput$output"); can also be replaced with:
my $cmd = q#perl -F, -aane 'chomp @F; print join ",\t", @F[ grep(! /^(# . "$var1|$var2|$var3" . q!)$/, 0..$#F)] ' ! . " $spectralfile > $diroutput$output "; warn $cmd; system( $cmd );
see perlrun for details. also try this static cmd on your spectralfile:
perl -F, -lane 'chomp @F; print join ",\t", @F[ grep(! /^(1|10|6)$/, 0 +..$#F)] ' blah.txt