Link to home
Start Free TrialLog in
Avatar of Grant Rogers
Grant RogersFlag for United Kingdom of Great Britain and Northern Ireland

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.

@diff{ @list2 }= ();;

Open in new window


@list1 = (1, 2, 3, 4, 5);
@list2 = (2, 3, 4);;
@diff{ @list2 }= ();;

print grep !exists($diff{$_}), @list1;;
1 5

Open in new window


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
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Grant Rogers

ASKER

Thanks for the quick response, I think I was correct with my original assumption than, he is populating a hash with keys from @list2 and assigning the values to empty for each key.  

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.
> populating a hash with keys from @list2 and assigning the values to empty for each key.
correct