control structure - Regular expression handling in elsif block in perl -
gmf file:
tstartcustevsummrow_gprs custevsummrow_gprs gprs - subscriber package (paygo)|93452|mb|240|33952 custevsummrow_gprs gprs - mbb plan (paygo)|93452|mb|160|20128 tendcustevsummrow_gprs tstartcustevsummrow_gprs_simple custevsummrow_gprs_simple gprs - lte roam package|1529551|mb|85|260536 custevsummrow_gprs_simple gprs - lte roam package|65461|mb|20000|1309252 tendcustevsummrow_gprs_simple
code:
if ( $line =~ m/^(custevsummrow_simple|custevsummrow_gprs_simple|custevsummrow_gprs|custevsummrow|custprodsummrow)\s(.*?)\|.*\|(.*?)$/) { $tag = $1; $linetxt = $2; $amt = $3; if ( $tag =~ m/^(custevsummrow|custevsummrow_simple)/ ) { print "processing validations"; } else { print " mapping failed"; } elsif ( $tag =~ m/^(custevsummrow_gprs|custevsummrow_gprs_simple)/ ) { if () { #it has validations. } else { #failed; } } }
when try process elseif
condition not able process. please me out in solving issue?
output:
unable map:custevsummrow_gprs | gprs - data lte package roaming | 34646.2272 unable map:custevsummrow_gprs | gprs - lte dealer1 package roaming | 34609.3312 unable map:custevsummrow_gprs_simple | gprs - simple subscriber package 3 | 32.1899 unable map:custevsummrow_gprs_simple | gprs - simple talk , text package | 0.2702
i recommend change of approach. rather individually matching specific parts of line, , having on , on again, tokenize @ start. is, split grammatical pieces. once parsing out of way, easier work with.
an example english, parse things "go store", "you go store", "i went store", "we going store", search go|going|went
@ various positions, or can break subject (go), verb (you), object (store) , work them.
it looks you'e got |
delimited set of fields (your post conflicts on detail, adjust necessary). split on pipe tokenize.
my($tag, $description, $amount, $units, $limit, $something) = split m{\|}, $line;
now can work $tag
without having further parsing on whole line.
if( $tag eq 'custevsummrow' or $tag eq 'custevsummrow_simple' ) { ... } elsif( $tag eq 'custevsummrow_gprs' or 'custevsummrow_gprs_simple' ) { ... }
you can make code simpler pushing tag logic subroutine.
sub is_tag_of_type { my($tag, $type) = @_; return 1 if $type eq 'gprs' , $tag =~ /gprs/; return 1 if $type eq 'simple' , $tag =~ /simple/; ... }
or maybe tag has own little grammar , can split tokens.
sub tokenize_tag { $tag = shift; @tokens = split /_/, $tag; return map { $ _ => 1 } @tokens; }
then code process line looks this.
my($tags, $description, $amount, $units, $limit, $something) = split m{\|}, $line; %tags = tokenize_tags($tags); if( $tags{gprs} ) { ... } else { ... }
Comments
Post a Comment