in reply to Re: Arrays merges and redundancies
in thread Arrays merges and redundancies
I tried.
Sometimes I see List::Util and List::MoreUtil at Monk. I should see them.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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Arrays merges and redundancies
by Marshall (Canon) on Mar 31, 2012 at 04:24 UTC | |
by remiah (Hermit) on Mar 31, 2012 at 13:45 UTC | |
by Anonymous Monk on Mar 31, 2012 at 10:27 UTC | |
by Anonymous Monk on Mar 31, 2012 at 10:49 UTC |