my %flag = (); while () { s/\.\d+\s*=\s*/_/; # Format the string to match nicer $flag{$1}++ if /(VLAN|if(?:Oper|Admin)Status_(?:up|down))/; } # Now access $flag{VLAN}, $flag{ifOperStatus_up}, etc.