in reply to Can this code be optimized further?

Another way that works from a string instead of from an array:

use strict; use warnings; my @temp = qw( a_1 b_1 a_2 a_3 a_4 b_2 a_5 b_3 a_6 b_4 ); local $_ = join(' ', @temp); my @a = /a_(\d+)/g; # or: my @a = /\ba_(\d+)\b/g; my @b = /b_(\d+)/g; # or: my @b = /\bb_(\d+)\b/g; $, = ", "; $\ = "\n"; print(@a); print(@b); __END__ output ====== 1, 2, 3, 4, 5, 6 1, 2, 3, 4