Over a million developers have joined DZone.

Perl: Sorted Union Of Two Arrays

DZone's Guide to

Perl: Sorted Union Of Two Arrays

Free Resource
Create a union of two arrays and sort the array.

my @union = array_union_sort(\@first, \@second);

sub array_union_sort {
    my $aa = shift;
    my $bb = shift;
    my %union;
    my $e;
    foreach $e (@$aa) { $union{$e} = 1; }
    foreach $e (@$bb) { $union{$e} = 1; }
    my @union = keys %union;
    @union = sort { $a <=> $b } @union;
    return @union;

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}