anirban.adhik...@gmail.com (Anirban Adhikary) writes: > Hi List > I have the following array --- > ('1900-0','1900-1','NULL','NULL','1900-2','1900-4','1902-5','1902-6','1902-7','1902-8'); > There are two part for each element separated by a dash. > first one known as earfcn and second one is pcid . > The requirement is For the same “earfcn”, concatenate the “pcid” values > using "&" separator between values. > so the output will be like this > EARFCN=1900,PCID=0&1&2&4; > EARFCN=1902,PCID=5&6&7&8;
Use a hash from earfcn to an array of pcids.
#!/usr/bin/perl use v5.14; use warnings; my @arr = ('1900-0','1900-1','NULL','NULL','1900-2','1900-4','1902-5','1902-6','1902-7','1902-8'); my %hash; for (@arr) { my ($earfcn, $pcid) = split /-/; next unless $pcid; # Skip the NULLs $hash{$earfcn} //= []; push @{$hash{$earfcn}}, $pcid; } for my $earfcn (keys %hash) { say "EARFCN=$earfcn,PCID=", join '&', @{$hash{$earfcn}}; }
-- Marius Gavrilescu
signature.asc
Description: PGP signature