Thanks,
Marshall for your recommendation.
Well, as I said, I first thought that Marshall's idea of a solution was good, but since I felt that is was probably a bit complicated. I was hoping to make something simpler. As I said, I ended up doing independently something essentially similar (although written differently and using an additional $flag variable, which isn't really necessary, as Marshall has shown).
Trying to simplify (or at least shorten) the code I tried to use a ternary conditional, something along these lines:
push $flag ? @temp : @result, $num;
But the compiler doesn't like this. Even adding parentheses does not seem to help.
So that's why I next came to the idea of pushing the values to a variable containing a reference to either the @temp or the @result array, depending on whether we are in a 6..7 range or not. I think this is pretty clean and clear, and quite concise, but using references this way may be beyond the knowledge of a beginner (although the OP uses an array reference).