... if ( $str =~ / / ) { print "no underscore\n"; } else { print "with underscore\n"; } ... #### ... my $str = $ARGV[0] || '|78187980|ref|NM_0'; # original string my $cnt = $str =~ tr/_//; # count the number of underscores print 'with ' . ($cnt || 'no') . " underscore(s)\n"; # print result depending on count ...