Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^5: Create output from Perl hash

by poj (Monsignor)
on Feb 13, 2018 at 17:40 UTC ( #1209077=note: print w/replies, xml ) Need Help??


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

My advice would be as step 1 - extract the data you need into a structure. Multiple records with a common key suggests a HashOfArrays. For example

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $key1 = 'MSISDN'; my $key2 = 'CF'; my $sep = ','; # input my $rec = {}; while (my $line = <DATA>){ next if $line =~ /BEGINFILE/; #skip first line chomp $line; $line =~ s/^\s+|;$//g; # remove leading whitespace and ; if ($line =~ /SUBBEGIN/){ $rec = {}; # start new record } elsif ($line =~ /SUBEND/){ if (defined $rec->{$key1} && defined $rec->{$key2}){ output_record($rec) ; } } else { my ($key,$value) = split /=/,$line; push @{$rec->{$key}},$value if ($key); } } # output sub output_record { my $rec = shift; print Dumper \$rec; } __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

If that works, then step 2 work on your transformation and output code to replace Dumper.

sub output_record { my $rec = shift; # print Dumper \$rec; my $MSISDN = $rec->{$key1}[0]; # single my @CF = @{$rec->{$key2}}; # multiple for (@CF){ s{(CF.*-ALL-PROV)-NONE.*}{$1-1/1/1/0}; s{(CF.*-TS10-(?:REG|ACT))-91(\d*).*}{$1-1/1/1/0-$2}; } print join $sep,$MSISDN,@CF; print "\n"; }
poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-06-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (125 votes). Check out past polls.

    Notices?