in reply to Re: Parsing file in Perl post processing
in thread Parsing file in Perl post processing

Sorry with my limited exposure to perl I am trying to understand what you are doing in these lines of code

my $z = qr{(?:[A-Z_]+:|$)}; my %hash = map { $_, ($string =~ m/$_:(.+?)\s*$z/)} @keys

How does qr work could you please explain what you are doing?

How is the hash created, I don't understand map and $_, and the $string part

Thanks Tom

Replies are listed 'Best First'.
Re^3: Parsing file in Perl post processing
by GotToBTru (Prior) on Sep 13, 2015 at 09:15 UTC

    By using qr{} I am telling Perl the string inside will be used in a regex. See http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators. The most common reason to do this is to save time when you are using the same regex over and over; I use it because it often seems to make sure Perl interprets the regex in the way I expect.

    map {block} @array returns an array created by executing {block} once for each element of @array, each time assigning one value to $_.

    @a=(1,2,3); @a_plus_1 = map { $_ + 1 } @a;

    The block, which in this example is $_ + 1, will be execute 3 times, once for each value in @a. It will put the results also in an array, so the values in @a_plus_1 will be (2,3,4).

    In my solution, I have the map return two values separated by a comma. This is one way to define a hash. You can see this using the debugger:

    perl -d -e 1 Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> %h=(1,2,3,4) DB<2> print $h{1} 2
    Dum Spiro Spero
      Thanks for the explanation, but I am still a bit confused

      about how you exactly created the hash.

      my %hash = map { $_, ($string =~ m/$_:(.+?)\s*$z/)} @keys;

      what I understand is that you use map to create a new list from the @keys array. Am I correct in saying that $_ contains the first entry from @keys array? next part I don't understand at all could you please walk through the code?

        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