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?
Grant RogersMonitoring ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
@diff{ @list2 } is hash slice, equivalent to @diff{ 2,3,4 } or ($diff{2},$diff{3},$diff{4})
see
perldoc perldata

There is no array reference to dereference, there is no array @diff, and by the way, the double ;; is superfluous

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Grant RogersMonitoring ConsultantAuthor Commented:
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.
ozoCommented:
> populating a hash with keys from @list2 and assigning the values to empty for each key.
correct
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.