On Wed, 6 May 2015 12:49:53 +0530
Anirban Adhikary <anirban.adhik...@gmail.com> wrote:

> 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;
> 
> I am not able to generate any idea how to adress this isuue. Please
> give me some starting point.
> 
> Best Regards
> Anirban.

Unlike everyone else, I would used nested hashes. Why? Because then the
code can hand duplicates.

#!/usr/bin/env perl

use strict;
use warnings;

my @data =
('1900-0','1900-1','NULL','NULL','1900-2','1900-4','1902-5','1902-6','1902-7','1902-8');
my %hash = ();

# load the data structure
for my $item ( @data ){
  if( my ( $earfcn, $pcid ) = $item =~ m{ \A ( \d+ ) \- ( \d+ ) \z }msx ){ 
      $hash{$earfcn}{$pcid} ++;
  }
}

# output the data
for my $earfcn ( sort { $a <=> $b } keys %hash ){
  my @pcids = ();
  for my $pcid ( sort { $a <=> $b } keys %{ $hash{$earfcn} } ){
    for my $count ( 1 .. $hash{$earfcn}{$pcid} ){
      push @pcids, $pcid;
    }
  }
  print "EARFCN=$earfcn,PCID=", join( q{&}, @pcids ), "\n";
}


-- 
Don't stop where the ink does.
        Shawn

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to