#!/usr/bin/perl use strict; use warnings; my $config = { count => { x => 9, y => 14, }, other => { a => 1, b => 2, } }; my $some_variable = 'x'; die "bad count" unless exists $config->{count}{$some_variable}; for my $i (0 .. $config->{count}{$some_variable}) { #do some stuff; }