Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^7: Create output from Perl hash

by Laurent_R (Canon)
on Feb 14, 2018 at 11:24 UTC ( #1209128=note: print w/replies, xml ) Need Help??

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/;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1209128]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-05-24 19:53 GMT
Find Nodes?
    Voting Booth?