With the little that I know about Perl, I was surprised that this syntax doesn’t work:
if (exists $wid = $dict{$w}) {
print "$wid:$c\n";
}
It seems I can’t make the assignment $wid = $dict{$w} and check if it exists at the same time:
exists argument is not a HASH or ARRAY element or a subroutine at
createWordIndex.pl line 31.
Is there a way to do this kind of assignment? Or do I just need to check existence first and then make the assignment if the condition is true?
Are you trying to figure out if there’s a value there before you print it? Then you’ll have to do it this way:
If you know that the values stored will always be defined, you can do