Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^9: Create output from Perl hash

by gbwien (Sexton)
on Feb 14, 2018 at 13:30 UTC ( [id://1209137]=note: print w/replies, xml ) Need Help??


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

Actually I think this might work>

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

Replies are listed 'Best First'.
Re^10: Create output from Perl hash
by Laurent_R (Canon) on Feb 14, 2018 at 15:45 UTC
    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/;

      edited

      I am studying your program and one question I would like to ask you is the following. If the line does not match an MSISDN=value pair e.g. MSISDN=433336901235;, I would like to write to the line MSISDN=0 and then to continue processing the next line, but I am not certain how to do that? This scenario could also occur for other entries other than MSISDN but if I can understand how an example would work I could apply this later on to other parts of the code

      Looking at the input file again as an example:-

      <BEGINFILE> <SUBBEGIN IMSI=231111400010332; MSISDN=433336901235; ..... .. . <BEGINFILE> <SUBBEGIN IMSI=231111400010339; <----MSISDN is missing I would like +to write MSISDN=0 to the output file ... .. .
      use strict; use warnings; 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";

      the output would look something like this

      433336901235,....... additional entries

      MSISDN=0,....... additional entries

      Thanks you, graham

        The program I suggested used the MSISDN line to detect the beginning of a new block. This has to be changed if the MSISDN line can be absent from a record block. Here I changed the end block detection to matching <SUBEND, which simplifies a bit the code. Otherwise, we initialize a new line with "MSISDN=0" and change it to the MSISDN value if we find a MSISDN line. That's how it looks after these changes:
        use strict; use warnings; use strict; use warnings; my $line = "MSISDN=0"; while (<DATA>) { if (/<SUBEND/) { print "$line\n"; $line = "MSISDN=0"; } $line = $1 if /^\s*MSISDN=(\d+);/; if (/\s*CF=([\w-]+?-(?:NONE|\d+))/) { my $add = $1; $add =~ s{\d\d(\d+)$}{1/1/1/$1}; $add =~ s{NONE}{1/1/1/0}; $line .= ",$add"; } } __DATA__ <BEGINFILE> <SUBBEGIN IMSI=232191400010332; MSISDN=436906901235; CF=CFU-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO-N +O-NO-NO; CF=CFB-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO- +NO-NO-NO; CF=CFNRY-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-N +O-NO-NO-NO; CF=CFNRC-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO +-NO-NO-NO; CF=CFD-TS10-ACT-91436903000-YES-YES-25-YES-65535-YES-YES-NO-NO-NO +-YES-YES-YES-YES-NO; <SUBEND <BEGINFILE> <SUBBEGIN IMSI=232191400010339; MSISDN=436906901231; CF=CFU-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO-N +O-NO-NO; CF=CFB-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO- +NO-NO-NO; CF=CFNRY-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-N +O-NO-NO-NO; CF=CFNRC-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO +-NO-NO-NO; CF=CFD-TS10-ACT-91436903000-YES-YES-25-YES-65535-YES-YES-NO-NO-NO +-YES-YES-YES-YES-NO; <SUBEND <SUBBEGIN IMSI=232191400010339; CF=CFU-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO-N +O-NO-NO; CF=CFB-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO- +NO-NO-NO; CF=CFNRY-ALL-PROV-NONE-YES-YES-NONE-YES-65535-NO-NO-NO-NO-NO-NO-N +O-NO-NO-NO; CF=CFNRC-ALL-PROV-NONE-YES-NO-NONE-YES-65535-NO-NO-NO-NO-NO-NO-NO +-NO-NO-NO; CF=CFD-TS10-ACT-91436903000-YES-YES-25-YES-65535-YES-YES-NO-NO-NO +-YES-YES-YES-YES-NO; <SUBEND
        This prints the following output:
        $ perl msisdn.pl 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 436906901231,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 MSISDN=0,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
        which seems to be what you're looking for.

        Update: changed my $line; to my $line = "MSISDN=0"; following AnomalousMonk's correct comment in Re^15: Create output from Perl hash. Thanks to AnomalousMonk for that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found