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


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

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.

Replies are listed 'Best First'.
Re^8: Create output from Perl hash
by gbwien (Sexton) on Feb 14, 2018 at 13:15 UTC

    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/;
        I had not seen your latest post above when I replied below in Re^9: Create output from Perl hash. Your solution is probably fine if you're guaranteed that the number to be changed always starts with 91.

        BTW, you don't need to put 91 between parentheses (but then have to use $1 instead of $2):

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