-1

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 some validations";
    } else {
        Print " Mapping failed";
    } elsif ( $tag =~ m/^(CUSTEVSUMMROW_GPRS|CUSTEVSUMMROW_GPRS_SIMPLE)/ ) {
        if () {
            #It has to do some validations.
        } else {    
            #Failed;
        }
    }
}

When I try to process the elseif condition is not able to process. Could you please help me out in solving this issue?

Output:

Unable to map:CUSTEVSUMMROW_GPRS | GPRS - Data Only LTE Package Roaming | 34646.2272
Unable to map:CUSTEVSUMMROW_GPRS | GPRS - LTE Dealer1 Package Roaming | 34609.3312
Unable to map:CUSTEVSUMMROW_GPRS_SIMPLE | GPRS - Simple Subscriber Package 3 | 32.1899
Unable to map:CUSTEVSUMMROW_GPRS_SIMPLE | GPRS - Simple Talk and Text Package | 0.2702
Miller
  • 34,962
  • 4
  • 39
  • 60
user3274607
  • 19
  • 1
  • 1
  • 6
  • 3
    Code with `if()...else...elsif()...` can never execute the `elsif` part. It will execute one of the first two clauses. – AdrianHHH Sep 10 '14 at 18:51
  • 1
    Your output does not match your input. In your output there's a `|` between the tag and the tag. Your input and code say there's a space. – Schwern Sep 10 '14 at 19:58

2 Answers2

1

I would recommend a change of approach. Rather than individually matching specific parts of the line, and having to do this over and over again, tokenize it at the start. That is, split it into grammatical pieces. Once the parsing is out of the way, it will be much easier to work with.

An example from English, to parse things like "Go to the store", "You go to the store", "I went to the store", "We are going to the store", you could search for go|going|went at various positions, or you can break it up into subject (go), verb (you), object (store) and then work with them.

It looks like you'e got a | delimited set of fields (your post conflicts on this detail, adjust as necessary). Split on that pipe to tokenize.

my($tag, $description, $amount, $units, $limit, $something) = split m{\|}, $line;

Now you can work with $tag without having to do further parsing on the whole line.

if( $tag eq 'CUSTEVSUMMROW' or $tag eq 'CUSTEVSUMMROW_SIMPLE' ) {
    ...
}
elsif( $tag eq 'CUSTEVSUMMROW_GPRS' or 'CUSTEVSUMMROW_GPRS_SIMPLE' ) {
    ...
}

You can make the code simpler by pushing the tag logic into a subroutine.

sub is_tag_of_type {
    my($tag, $type) = @_;

    return 1 if $type eq 'GPRS'   and $tag =~ /GPRS/;
    return 1 if $type eq 'SIMPLE' and $tag =~ /SIMPLE/;
    ...
}

Or maybe the tag has its own little grammar and can be split into tokens.

sub tokenize_tag {
    my $tag = shift;

    my @tokens = split /_/, $tag;
    return map { $ _ => 1 } @tokens;
}

Then your code to process a line looks like this.

my($tags, $description, $amount, $units, $limit, $something) = split m{\|}, $line;
my %tags = tokenize_tags($tags);

if( $tags{GPRS} ) {
    ...
}
else {
    ...
}
Schwern
  • 153,029
  • 25
  • 195
  • 336
0

Writing if ... else ... elsif is a syntax error -- your code won't even run. Assuming that the elsif should be between the if and the else you have another problem: the regex in the if condition is more general than the one in the elsif condition. CUSTEVSUMROW will match anything that CUSTEVSUMMROW_GPRS or CUSTEVSUMMROW_GPRS_SIMPLE would. Swap the if and elsif blocks so that the specific check happens before the general one.

if ($tag =~ /^CUSTEVSUMMROW_GPRS/) {
    ...
}
elsif ($tag =~ /^CUSTEVSUMMROW/) {
    ...
}
else {
    ...
}
Michael Carman
  • 30,628
  • 10
  • 74
  • 122