perl - How does the grep operator work? -
I have an array and a hash. I want to match the keys of the head with the key of hash.
I got an expression to fix all the keys matching the array. It works, but I'm not sure I understood how it works. Can you explain what happens after array The code you got is more complex This could be / *: In your example expression, a threesome operator grep
@text and hash
% sys already Is defined only.
My @new_array = grep {$ sys {$ _} is present? $ sys {$ _}: 0} @text;
My @new_array = grep {$ sys {$ _}} @text; The
grep function processes a list - the contents of the
@text array - in a new list containing only the element Where the expression in the middle evaluates a true value, the default value is set to every list item in exchange for the expression list content so that the default value is
$ _ .
? which evaluates the item before
: If the first part is true or the value after it is incorrect, in your case, it checks whether each key is
% sys is in hash, and hash evaluates a lookup of entry if it exists, or 0 (a wrong value) if it is not functionally within a
grep Should be due to
$ sys {$ _} because
undef is that you have a non-denominator Key is also wrong. However, there is a caveat - if
% sys was tied to a pearl class and some hash functions were overridden, Different hash methods are called for
existing and hash lookups. Therefore there can be a difference in performance or behavior in that scenario.
Comments
Post a Comment