How can I see if a Perl hash already has a certain key?

后端 未结 5 1593
刺人心
刺人心 2020-12-15 15:01

I have a Perl script that is counting the number of occurrences of various strings in a text file. I want to be able to check if a certain string is not yet a key in the has

5条回答
  •  [愿得一人]
    2020-12-15 15:48

    Well, your whole code can be limited to:

    foreach $line (@lines){
            $strings{$1}++ if $line =~ m|my regex|;
    }
    

    If the value is not there, ++ operator will assume it to be 0 (and then increment to 1). If it is already there - it will simply be incremented.

提交回复
热议问题