my %dispatch = ( '#' => \&sub1, 1 => \&sub2, ); my @array = (1, 2, 3, '#', 6, 7, '#', 9, 10); # corrected typo! foreach (@array) { if( exists $dispatch{$_} ) { $dispatch{$_}->(); } else { sub3(); }