Grant Rogers
asked on
Can someone please explain what is happening with the declaration of the array @diff?
I do not know why the declaration below works. I haven't ever seen it written like this in any of the books.
It looks like he is initialising a hash with keys from list2 and values as an empty array, but he is not using a hash. I have run the code below and it works, so he hasn't made a typo. I also don't understand how he is able to dereference an array in the print line below.
Original source here: http://www.perlmonks.org/index.pl?node_id=919422
I would ask on the original thread, but the author made this comment in 2011.
Can anyone provide an explanation with a reference to the rule that allows this?
It looks like he is initialising a hash with keys from list2 and values as an empty array, but he is not using a hash. I have run the code below and it works, so he hasn't made a typo. I also don't understand how he is able to dereference an array in the print line below.
@diff{ @list2 }= ();;
@list1 = (1, 2, 3, 4, 5);
@list2 = (2, 3, 4);;
@diff{ @list2 }= ();;
print grep !exists($diff{$_}), @list1;;
1 5
Original source here: http://www.perlmonks.org/index.pl?node_id=919422
I would ask on the original thread, but the author made this comment in 2011.
Can anyone provide an explanation with a reference to the rule that allows this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> populating a hash with keys from @list2 and assigning the values to empty for each key.
correct
correct
ASKER
The hash slice explains the syntax, thank you :).
I also noticed the superfluous semi colons, I guess he was typing out the code quickly, but useful for other people to know who read this.