use strict; use warnings; use Data::Dumper; my @IDs = qw(Apple Apple Grape Banana Banana); my @Price = qw(5 5 2 3 4 ); my @Amount = qw(10 15 3 4 4 ); sub uniqjoin { my %seen; my $sep=shift; return join($sep, grep { !$seen{$_}++ } sort @_); } my ($idx, $id, %h); #to hash of array while( ($idx,$id)= each(@IDs) ){ push @{$h{$id}->{price}} , $Price[$idx]; push @{$h{$id}->{amount}}, $Amount[$idx]; } #concatenate arrays with '/' foreach my $id (keys %h){ $h{$id}->{price} = uniqjoin( '/', @{$h{$id}->{price}} ); $h{$id}->{amount} = uniqjoin( '/', @{$h{$id}->{amount}} ); } #print print join(' ', sort keys(%h)) . "\n"; print join(' ' , map{ $h{$_}->{price} } sort keys(%h) ) . "\n"; print join(' ' , map{ $h{$_}->{amount} } sort keys(%h) ) . "\n";