Sounds like a case where it would be much simpler NOT to use a repeating field. The same data could be placed in a related table with one record for each repetition. A portal filter or a match field could then be manipulated by your script to control which 5 records show in either a 5 record vertical portal or a 5 record horizontal portal.
To do this with a repeating field would be much more complex. The simplest option would be to add another repeating field of just 5 repetitions and use your script to copy 5 selected values from your repeating field into the repetitions of this added repeating field. I don't know if a calculation with a 5 repetition result might also work for this or not.