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 grep

array @text and hash % sys already Is defined only.

  My @new_array = grep {$ sys {$ _} is present? $ sys {$ _}: 0} @text;    

The code you got is more complex This could be / *:

  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 $ _ .

In your example expression, a threesome operator ? 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

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -