http://www.perlmonks.org?node_id=1209127


in reply to Re^5: Create output from Perl hash
in thread Create output from Perl hash

I know, sorry for not being precious and thank you for your time again.

Also the 91436903000 should be 1/1/1/0-436903000

We should remove 91 from the beginning of 91436903000, so the result should look like this.

436906901235,CFU-ALL-PROV-1/1/1/0,CFB-ALL-PROV-1/1/1/0,CFNRY-ALL-PROV +-1/1/1/0,CFNRC-ALL-PROV-1/1/1/0,CFD-TS10-ACT-1/1/1/436903000 436767817451,CFU-ALL-PROV-1/1/1/0,CFB-ALL-PROV-1/1/1/0,CFNRY-ALL-PROV +-1/1/1/0,CFNRC-ALL-PROV-1/1/1/0,CFU-TS10-ACT-1/1/1/4369050045021,CFD- +TS10-REG-1/1/1/91436903000 4369060900384,CFU-ALL-PROV-1/1/1/0,CFB-ALL-PROV-1/1/1/0,CFNRY-ALL-PROV +-1/1/1/0,CFNRC-ALL-PROV-1/1/1/0,CFD-TS10-ACT-1/1/1/436903000

I would like to ask you a couple of questions about your code so I can learn

my $line; while (<DATA>) { if (/^\s*MSISDN=(\d+);/) { print "$line\n" if defined $line; $line = $1 ; } if (/\s*CF=([\w-]+?-(?:NONE|\d+))/) { my $add = $1; $add =~ s{(\d+)$}{1/1/1/$1}; $add =~ s{NONE}{1/1/1/0}; $line .= ",$add"; } } print "$line\n";

In the first if statement you search for MSISDN and a group containing 1 or more digits. I do not understand what you are doing with print "$line\n" if defined $line;

In the second if statement you use a non capturing group, could you please explain?

Could you please explain the use of curly braces in your code ?

Replies are listed 'Best First'.
Re^7: Create output from Perl hash
by Laurent_R (Canon) on Feb 14, 2018 at 11:24 UTC
    Hi gbwien,

    The program is populating the $line variable progressively. First with the MSISDN, and then with the data from the CF lines.

    When the program meets a new MSISDN line, it means that we are starting to process a new record block; we need to print out everything that has been stored in $line and reinitialize $line for the next record block. However, we don't want to print $line the first time (because we haven't started yet to populate it).

    There is a capture using ([\w-]+?-(?:NONE|\d+)). I simply don't need the nested parentheses (?:NONE|\d+) to capture anything, there are there just to clarify what the alternative match should be (either NONE or a sequence of digits).

    As for curlies in the s/// operator, I am using them because the substitution strings contain slashes that would be interpreted as part of the substitution operator.

    Using s{...}{...} instead of s/.../.../ makes it possible to avoid having to escape the / in the sustitution strings.

      Thanks for the explanation. I would like to remove 91 from the beginning of the string e.g. 91436903000 should be 436903000, I am not sure how your code should be modified to allow for the substitution?

      if (/\s*CF=([\w-]+?-(?:NONE|\d+))/) { { my $add = $1; $add =~ s/(\d+)$/1\/1\/1\/$1/; $add =~ s/NONE/1\/1\/1\/0/; $line .= ",$add"; }
        To remove the first two digits, try changing this:
        $add =~ s/(\d+)$/1\/1\/1\/$1/;
        to this:
        $add =~ s/\d\d(\d+)$/1\/1\/1\/$1/;

        Actually I think this might work>

        $add =~ s/(91)(\d+)?/$2/;