#!/usr/bin/perl
#
# eg9: freq & indexlist example, idiomatic version..
#

use strict;
use warnings;

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

my %indexlist; # build array element -> list of positions in original array
my %freq;      # build array element -> frequency of that element

map {
	$freq{$array[$_]}++;
	my $aref = ($indexlist{$array[$_]} ||= []);
	push @$aref, $_;
} 0..$#array;

# build @uniq, all unique elements of @array
my @uniq = grep { $freq{$_} == 1 } @array;
my $str = join(',',@uniq); print qq($str\n);

# now lets use indexlist too...
my $aref = $indexlist{5};
$str = join(',',@$aref);
print qq(posns of 2 = $str\n)
