map executes the contents of {} once for each element in @keys. Here is (part of) what is in @keys:

0 'THREAD_ID' 1 'CDR_TYPE' 2 'SUB_TIME'

map executes the contents of the block once for each value in @keys.

{ 'THREAD_ID', ('THREAD_ID:1bf1d698 CDR_TYPE:AO SUB_TIME:240815144 +127 DEL...' =~ m/THREAD_ID:(.+?)\s*$z/)} { 'CDR_TYPE', ('THREAD_ID:1bf1d698 CDR_TYPE:AO SUB_TIME:240815144 +127 DEL...' =~ m/CDR_TYPE:(.+?)\s*$z/)} { 'SUB_TIME', ('THREAD_ID:1bf1d698 CDR_TYPE:AO SUB_TIME:240815144 +127 DEL...' =~ m/SUB_TIME:(.+?)\s*$z/)}

Each statement has two parts, separated by the comma. The first part returns the literal value of the key.

The second part returns the matching value in the string, if any. $z has been defined to match a key, ie, any combination of uppercase letters and underscores followed by a colon. So in effect, I am saying "look in that long string for THREAD_ID followed by a colon, and then remember anything you find between there and the next thing that looks like a key. Then do it again for CDR_TYPE, SUB_TIME, etc."

After all three of these have executed, map returns ('THREAD_ID,'1bf1d698','CDR_TYPE','AO','SUB_TIME','240815144127'). If I assign this list to a hash, Perl understands to treat the first element of each pair as the key of the hash and the second as the value.

%hash = ('THREAD_ID,'1bf1d698','CDR_TYPE','AO','SUB_TIME','240815144127')

is the same as:

$hash{'THREAD_ID'} = '1bf1d698'; $hash{'CDR_TYPE'} = 'AO'; $hash{'SUB_TIME'} = '240815144127';

By the way, that ability to say "match something that looks like this" is really the power of regexes. Computers are fantastic at matching things, and most languages have a built-in ability to "find 'X' in 'WXYZ'". Regexes let you say "find the first lowercase vowel that comes after the third consonant unless preceded by an exclamation point unless also followed by a question mark."

Dum Spiro Spero

In reply to Re^5: 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.