in reply to Re^2: Removing Duplicate Array Elements!!!
in thread Removing Duplicate Array Elements!!!

Hello SridharKesari, and welcome to the Monastery!

The hash method has one drawback: it doesn’t preserve the order in which the words first appear in the original array. Here’s a simpler approach that does:

#! perl use strict; use warnings; use Data::Dump; use List::MoreUtils 'uniq'; dd uniq qw{ sridhar sridhar guru chan guru hubli hubli hubli hurryo };

Output:

22:48 >perl 1381_SoPW.pl ("sridhar", "guru", "chan", "hubli", "hurryo") 22:48 >

And here’s the sorted equivalent:

dd sort +uniq qw{ sridhar sridhar guru chan guru hubli hubli hubli hur +ryo };

(The + is needed here to tell Perl that uniq supplies the values to be sorted, not the sort comparison routine. See the section beginning “Warning: syntactical care is required...” in sort.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,