use strict; use warnings; my @clusters = ( { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 16, 7 => \&two, 4 => \&three }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two } ); foreach my $cluster ( @clusters ) { # decrement count by one to account for arrays starting at zero my $machine_count = $cluster->{ total } - 1; for my $machine ( 0 .. $machine_count ) { if ( ! exists $cluster->{ $machine + 1 } ) { &one; } else { &{ $cluster->{ $machine + 1 } }; } } } sub one { print "First sub\n"; } sub two { print "Second sub\n"; } sub three { print "Third sub\n"; }