my %opts; foreach (@switches) { # init the array for each $opts{$_} = []; } my $curswitch = ''; foreach (@values) { $curswitch = $_ and next if exists $opts{$_}; push @{$opts{$curswitch}}, $_; }