addressalign-toparrow-leftarrow-leftarrow-right-10x10arrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblightning-boltlinklocation-pinlockm-swarmSearchmailmediummessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstar-shapestartickettrashtriangle-downtriangle-uptwitteruserwarningyahooyoutube

Re: [perl-108] multiple city subst

From: Timm M.
Sent on: Monday, January 12, 2015, 1:49 PM
foreach my $key ( keys %city_subs ) {
   $city_regex{$key} = qr/\b$keyb/;
}

That would still compile the regex once each time through the loop, so it doesn't really gain anything.  The compiling the regex would need to be done before you hit the loop.  Hash keys have to be plain strings, so you'd have to alter the datastructure to make this work.  Even then, you'd only see gains if you're going to hit that loop multiple times in the same run.

On Mon, Jan 12, 2015 at 1:39 PM, david delikat <[address removed]> wrote:
I’m curious what the perl compiler does with:

my %city_subs = qq("DEQUEEN", "DE QUEEN",
      "ELDORADO", "EL DORADO", 
… 

the ‘qq’ at the front of the list should turn it into a single string which should
cause an error on assigning to the hash…

I think you want an ‘e’ modifier on your substitution so that t will
eval the substituted text

  $field[8] =~ s/\b$bad_city\b/$city_subs{$bad_city}/ie 

if you want to optimize it try something like this:

foreach my $key ( keys %city_subs ) {
   $city_regex{$key} = qr/\b$keyb/;
}

better yet, put an anonymous array in the hash table
so you don’t have so many hash lookups…

foreach my $key ( keys %city_subs ) {
   $city_sub{$key} = [ qr/\b$key\b/, $city_sub{$key} ];
}

also, if you do the join, don’t forget to insert the ‘\b’
anchors…

my $bad_cities = join("|”, map { ‘\\b’.$_.’\\b' } keys %city_subs);  #create piped list of cities: 

using a bulk substitution like that can cause unforeseen side effects if 
there is any duplication in the substitution strings…

-dav

On Jan 12, 2015, at 11:03 AM, Andy Bach <[address removed]> wrote:

A friend was trying:

The bold line is the one thing I can't yet figure out:
================
my $citysub = <<'EOF';
s/DEQUEEN/DE QUEEN/;
s/ELDORADO/EL DORADO/;
...                # there are about 100 such substitution strings
EOF

and then trying to figure out how to apply all those subst. commands on a single data field, in a clean up effort.

I came up w/
>> So, what's the proper syntax to insert a multi-line substitution string and apply it toward a single array value?

> Hmm, you might just use the data pairs differently
my %city_subs = qq("DEQUEEN", "DE QUEEN",
      "ELDORADO", "EL DORADO",
...

foreach my $bad_city ( keys %city_subs ) {
  $field[8] =~ s/\b$bad_city\b/$city_subs{$bad_city}/i
}

> Hmm, might get away with after the def :
my $bad_cities = join("|", keys %city_subs);  #create piped list of cities:

# in data loop a single match/replace:
$field[8] =~ s/($bad_cities)/$city_subs{$1}/i;

> untested of course.

and maybe adding an "o" (or using qr()) to "optimize" but I'm not sure I'm not missing something.  Suggestions?
--

a

Andy Bach,
[address removed]
[masked] cell
[masked] wk




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Andy Bach ([address removed]) from MadMongers (Madison Perl Mongers).
To learn more about Andy Bach, visit his/her member profile
To report this message or block the sender, please click here
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by david delikat ([address removed]) from MadMongers (Madison Perl Mongers).
To learn more about david delikat, visit his/her member profile
To report this message or block the sender, please click here
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

People in this
group are also in: