senthil_v has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Here i have written one code. For find the content from aray as per the pattern and i want push all elements to be stored in array. here below I have an one array that array which contains two element such as followed by .

here i have find OC192-11,(My pattern = \"((A-Z0-9+)\-(\d+)),/gis). So i want push all the oc192-11, oc192-12...etc. But mode code return only the 2 elements that is

My current code o/p

$VAR1 = 'OC192-11';

$VAR2 = 'OC192-12';

##@all_metrics contains the below array . foreach my $each_metrics_grp(@all_metrics) { $each_metrics_grp =~ s/RTRV(.*)//gi; #my @sub_metrics = split(/'\n'/, $each_metrics_grp) +; #my ($matched_true) = ( grep m/\"(([A-Z0-9]+)\-(\ +d+)),/gsi, @sub_metrics); #print "$1\n"; #print "@sub_metrics"; if( $each_metrics_grp =~m/\"(([A-Z0-9]+)\-(\d+)),/gis) { push (@all_head,$1); #print "matched:[$1]\n"; }else{ #print "not matched\n"; }

@all_metrics contains $VAR1 = 'RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-OM3500-1 09-02-12 03:46:54 M 1898 COMPLD "OC192-11,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" >';

$VAR2 = 'RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-OM3500-1 09-02-12 03:46:56 M 1898 COMPLD "OC192-12,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-28,STS3C:CVP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-28,STS3C:UASP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-28,STS3C:FCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:CVP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:ESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3C:SESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" ;';

Replies are listed 'Best First'.
Re: Find the array element as per the pattern.
by davido (Cardinal) on Jun 07, 2011 at 09:33 UTC

    You want to push onto an array all occurrences of OC\d+-\d+, right?

    use Modern::Perl; my @found; while( <DATA> ) { chomp; push @found, m/(OC\d+-\d+)/g; } say $_ for @found; __DATA__ RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-O +M3500-1 09-02-12 03:46:54 M 1898 COMPLD "OC192-11,OC192:CVS,0,COMPL,N +END,RCV,1-DAY,02-11,00-00,1" "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-D +AY,02-11,00-00,1" "OC192-11,OC192:PSCW,0,COMPL,NEND,RCV,1-DAY,02-11,0 +0-00,1" "OC192-11,OC192:PSCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "O +C192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC1 +92:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSCW,0,C +OMPL,NEND,RCV,1-DAY,02-11,00-00,1" > RTRV-PM-ALL:ADEL-OM3500-1:ALL:1898::,0-UP,NEND,,1-DAY,02-11,,;| ADEL-O +M3500-1 09-02-12 03:46:56 M 1898 COMPLD "OC192-12,OC192:PSCP,0,COMPL, +NEND,RCV,1-DAY,02-11,00-00,1" "OC192-12,OC192:PSD,0,COMPL,NEND,RCV,1- +DAY,02-11,00-00,1" "OC192-11-28,STS3C:CVP,0,COMPL,NEND,RCV,1-DAY,02-1 +1,00-00,1" "OC192-11-28,STS3C:UASP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00 +,1" "OC192-11-28,STS3C:FCP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC1 +92-11-145,STS3C:CVP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-1 +45,STS3C:ESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" "OC192-11-145,STS3 +C:SESP,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" ;

    Dave

    A reply falls below the community's threshold of quality. You may see it by logging in.