Wednesday, October 10, 2012

removing duplicate and empty elements from a perl array

Just another snippet not to remember.... removing the non-unique elements and the empty (null) elements.
sub uniq {
    return keys %{{ map { $_ => 1 } @_ }};
}

@my_array = ("one","two","three","two","three");
print join(" ", @my_array), "\n";
print join(" ", uniq(@my_array)), "\n";

No comments:

Post a Comment