use feature "switch"; for (to be determined) { when ((s/(\b[A-E]{3}\b)/XXX/g)) {@array[i,1] = $1} when (s/((Science)|(science))/XXX/g) {@array[i,2] = $1} when (s/((Math)|(math))/XXX/g) {@array[i,3] = $1} when - for all remaining subjects... when (s/((first|First)\W+(?:\w+\W+){1,10}?([A-E]{3})) #or some similar REGEX returning only the first year entry grades default {} }