http://www.perlmonks.org?node_id=878267

puterboy has asked for the wisdom of the Perl Monks concerning the following question:

I need to merge the elements of several arrays without repeats. Currently, I am using hash slices as follows:

my %temphash; @temphash{@array1, @array2} = (); my @mergedkeys = keys %temphash

I would love to eliminate the temporary hash by using anonymous hashes. But I seem to be missing the right magical combination of braces, ampersands, and percent signs to make it work. For example, I tried to no avail various things like:

 my @mergedkeys = keys %{{@array1, @array2}}

Similarly, is there any way to eliminate the temporary hash when I want to delete any elements of array2 appearing in array1 (along with eliminating any duplicates)? Currently, I am using the following code with a temporary hash:

my %temphash; @temphash{@array1} = (); delete @temphash{@array1}; my @newarray = keys %temphash;