my $key; while () { if ( /^(interface Vlan(\d+))/ ) { $key = $1; $hash{$1} = $2; } delete $hash{$key} if /no ip address/; }