in reply to sanity check

does this code look right

No. In fact, I can't even tell what you're trying to do.

Start by adding use strict; use warnings;