---------- Forwarded message ----------
From: Shlomi Fish <shlo...@shlomifish.org>
Date: Wed, May 6, 2015 at 11:31 AM
Subject: Fw: Perl array question
To: shlo...@gmail.com




Begin forwarded message:

Date: Wed, 6 May 2015 11:04:30 +0300
From: Shlomi Fish <shlo...@shlomifish.org>
To: beginners <beginners@perl.org>
Subject: Re: Perl array question


Hi Anirban,

please reply to all recipients.

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.
>

Use a hash of arrays/etc:

< CODE >
#!/usr/bin/perl

use strict;
use warnings;

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

my %earfcns;

foreach my $item (@arr)
{
    if ($item ne 'NULL')
    {
        if (my ($ear, $pcid) = $item =~ /\A([0-9]+)-([0-9]+)\z/)
        {
            push @{ $earfcns{$ear} } , $pcid;
        }
        else
        {
            die "Invalid item <<$item>>!";
        }
    }
}

foreach my $ear (sort { $a <=> $b } keys(%earfcns))
{
    print "EARFCN=$ear,PCID="
        . join("&", sort { $a <=> $b } @{$earfcns{$ear}})
        . ";\n"
        ;
}

< END CODE >

For more information, see:

* http://perl-begin.org/topics/hashes/

* http://perl-begin.org/topics/references/

(*NOTE*: perl-begin.org is a site that I happen to maintain).

Regards,

        Shlomi Fish

> Best Regards
> Anirban.



--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
http://www.shlomifish.org/humour/bits/facts/Summer-Glau/

The prefix “God Said” has the extraordinary logical property of converting
any
statement that follows it into a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .


--
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

NASA Uses COBOL.
    — http://is.gd/ClKAz5

Please reply to list if it's a mailing list post - http://shlom.in/reply .



-- 
------------------------------------------
Shlomi Fish http://www.shlomifish.org/

Chuck Norris helps the gods that help themselves.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Reply via email to