#!/usr/bin/perl
#
# eg10: separate functions example, first version..
#

use strict;
use warnings;
use Function::Parameters qw(:strict);


#
# @uniq = unique_values( @array ):
#	Deliver all non-repeated values from @array
#	in the SAME ORDER they were present in @array
#
fun unique_values( @array )
{
	my %freq; $freq{$_}++ for @array;  	   # array element -> frequency
	my @uniq = grep { $freq{$_} == 1 } @array; # @uniq, unique elements
	return @uniq;
}

#
# @nonuniq = distinct_nonunique_values( @array ):
#	Deliver all repeated (non-unique) values from @array
#	once each (i.e. distinct), in the SAME ORDER as they
#	were first found in @array
#
fun distinct_nonunique_values( @array )
{
	my %freq; $freq{$_}++ for @array;  	 # array element -> frequency
	my %seen;				 # elements we've already seen
	my @nonuniq = grep 			 # distinct non-unique elements
		{ $freq{$_} > 1 && ! $seen{$_}++ } @array;
	return @nonuniq;
}

#
# @distinct = distinct_values( @array ):
#	Deliver all distinct values from @array, 
#	in the SAME ORDER as first found in @array.
#
fun distinct_values( @array )
{
	my %seen;				     # elements already seen
	my @distinct = grep { ! $seen{$_}++ } @array;# distinct elements
	return @distinct;
}

# main program

my @array = ( 17, 5, 3, 17, 2, 5, 7, 6, 6, 10, 3 );

my @uniq = unique_values(@array);
my @nonuniq = distinct_nonunique_values(@array);
my @distinct = distinct_values(@array);

my $str = join(',',@uniq); print qq(uniq: $str\n);
$str = join(',',@nonuniq); print qq(nonuniq: $str\n);
$str = join(',',@distinct); print qq(distinct: $str\n);
