I am thinking there are no embedded spaces in the field names by the otherwise consistent use of underscore. But lines 12 and 13 of the example output do confuse things. Can you clarify?

This almost works (can't get value of last key):

use strict; use warnings; use Data::Dumper; my $string = 'THREAD_ID:1bf1d698 CDR_TYPE:AO SUB_TIME:240815144127 + DEL_TIME:240815144127 OA_ADDR:5.0.OTSDC PRE_TRANS_OA:5.0.OTSDC + DA_ADDR:1.1.966555696176 PRE_TRANS_DA:1.1.966555696176 ORIG_LOC +N:10.100.80.7/7220 ORIG_IDNT:OTS A2P DEST_LOCN:173.209.195 +.44/8341 DEST_IDNT:Syniverse A2P I_ERR:0.0 PPS_ID: PPS_PROFIL +E:AO Submission - OA charged PPS_ERR:1.0 O_ERR:0.0 SILO: + MSG_LEN:22 SEG_NUM:1 of 1 DLV_ATT:0 END_POINT:ESME FINA +L_STATE:DELIVERED REG_DEL:1'; my (@keys) = ($string =~ m/([A-Z_]+):/g); my $z = qr{(?:[A-Z_]+:|$)}; my %hash = map { $_, ($string =~ m/$_:(.+?)\s*$z/)} @keys; print Dumper(\%hash);

Output:

$VAR1 = { 'PPS_ID' => ' ', 'THREAD_ID' => '1bf1d698 ', 'DEST_IDNT' => 'Syniverse A2P ', 'CDR_TYPE' => 'AO ', 'ORIG_LOCN' => '10.100.80.7/7220 ', 'REG_DEL' => '1', 'SILO' => ' ', 'DEST_LOCN' => '173.209.195.44/8341 ', 'O_ERR' => '0.0 ', 'OA_ADDR' => '5.0.OTSDC ', 'PRE_TRANS_DA' => '1.1.966555696176 ', 'PPS_PROFILE' => 'AO Submission - OA charged ', 'I_ERR' => '0.0 ', 'DLV_ATT' => '0 ', 'ORIG_IDNT' => 'OTS A2P ', 'DA_ADDR' => '1.1.966555696176 ', 'MSG_LEN' => '22 ', 'FINAL_STATE' => 'DELIVERED ', 'SEG_NUM' => '1 of 1 ', 'SUB_TIME' => '240815144127 ', 'DEL_TIME' => '240815144127 ', 'PPS_ERR' => '1.0 ', 'END_POINT' => 'ESME ', 'PRE_TRANS_OA' => '5.0.OTSDC ' };

Update: with help of MidLifeXis, corrected regex in map to work even for last key:value pair in list. Update 2: changed final \s in map to \s*. Thanks to NetWallah and poj.

Dum Spiro Spero

In reply to Re: Parsing file in Perl post processing by GotToBTru
in thread Parsing file in Perl post processing by gbwien

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.