if ($resnum1=$resnum)
Inside the brackets is an assignment. You almost certainly don't want to do that but instead test equality. ie:
if ($resnum1 == $resnum)
== is for comparing numbers and eq is for comparing strings.
Is there any particular reason you use those massive regexes in preference to a simple split? That might make things a little clearer. Other tips: use strict and warnings, replace print ... exit with die and try to use consistent indenting to make your code more legible (this really does help).
Good luck.