in reply to Re^4: Parsing file in Perl post processing
in thread Parsing file in Perl post processing
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."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Parsing file in Perl post processing
by gbwien (Sexton) on Sep 16, 2015 at 08:26 UTC | |
by GotToBTru (Prior) on Sep 16, 2015 at 12:36 UTC |