in reply to printing specific lines

You could, for example, make a hash with keys equal to the line numbers you want to print (and corresponding true values) and use this to check whether a line should be printed as you loop.
(Update:Sorry, didn't see that this had been suggested.)
Are you collecting the line numbers from STDIN? (That's what it seems to be.) In that case, what does the "separate file" have to do with your code?
chas
(Update: Rewrote to fix error.)