in reply to Using a regex to replace looping and splitting

#!/usr/bin/perl # http://perlmonks.org/?node_id=1207789 use strict; use warnings; use Data::Dump 'pp'; $_ = 'special:1001:area_code:617|special:1001:zip_code:02205|special:1 +001:dow:0|special:1001:tod:14'; my %hash = /([^|:]+):([^|:]+)(?:\z|\|)/g; pp \%hash;

Replies are listed 'Best First'.
Re^2: Using a regex to replace looping and splitting
by mwb613 (Beadle) on Jan 24, 2018 at 01:20 UTC

    Thanks so much for the response!

    A few questions:

    1. Do you alias Dumper as pp for Pretty Print, as in Python?

    2. You use "=" rather than "=~" in the %hash assignment, how does it work in comparison

    3. Would the regex assignment work just as well if you stuck the "big string" in between the hash assignment and the regex for example and skipped the $_ manipulation:

    my %hash = $big_string = /([^|:]+):([^|:]+)(?:\z|\|)/g;

    4. Can you elaborate on the Regex a little? I have questions but I'm not really sure where to start. The last clause includes an escaped "z" for example

      Hi,

      'Dumper' is exportwd by Data::Dumper. 'pp' is exported by Data::Dump.

      I think you are missing the distinction of the implied '$_' variable via the regex. it would look like my %hash = $_ =~ /([^|:]+):([^|:]+)(?:\z|\|)/g; You can just drop the '$_' in this case like he did.

      For your third question, the proper form using the '$big_string' var (instead of '$_') would be my %hash = $big_string =~ /([^|:]+):([^|:]+)(?:\z|\|)/g;

      For your last question, the regex captures the 2 colon separated values that are immediately followed by the end of string, ('\z'), or by a pipe. He writes that using a non-capturing group, (?: ... ).