my $section = 0; my %sections = (1 =>[],2=>[]); while() { push @{$sections{$1}},$_ and next if /;section([12])/; warn "No section defined for: $_\n"; # cause warn gives you $. in }