Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^11: Create output from Perl hash

by gbwien (Sexton)
on Feb 19, 2018 at 15:22 UTC ( [id://1209478]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^12: Create output from Perl hash
by Laurent_R (Canon) on Feb 20, 2018 at 17:52 UTC
    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.

      Hi Laurent_R

      I do not understand how you set MSISDN=0 in your code, could you please explain it a little more to me? Thank you Graham

        Hi gbwien,

        the program I suggested does not set MSISDN=0, it does initialize the beginning of any new output line being built to the string "MSISDN=0". Then, if a line with a MSISDN is found in the input data, the new line beginning is changed to the value found for the MSISDN in the input record; if no MSISDN is found in the input, the initial value is kept and the output line will start with the string "MSISDN=0", which is what you want when there is no MSISDN line in the input.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-18 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found