Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
If you look at the output of the code; you'll see that once converted, the next regex converts it back..#!/usr/bin/perl -w use strict; my %subs = ( qr/(\d+)F/i => sub { warn "Matched F with $1\n"; return sprintf("%.u" , ($1 - 32) / 1.8) . 'C' }, qr/(\d+)C/i => sub { warn "Matched C with $1\n"; return sprintf("%.u" , ($1 * 1.8) + 32) . 'F' }, ); my $html = do { local $/; <DATA> }; warn $html; foreach my $key (keys %subs) { warn "checking key: $key\n"; $html =~ s/$key/$subs{$key}->()/eg; warn $html; } warn $html; __DATA__ convert 180F to C convert 180C to F convert 30" to cm etc
I assume the solution is to use the global match modifier \G within a while loop but can't get my head around it - if possible I'd like to maintain a hash of qr's to make the code easier to maintain as I extend it. Hope someone can help point me in the right direction! Thanksconvert 180F to C convert 180C to F convert 30" to cm etc checking key: (?i-xsm:(\d+)C) Matched C with 180 convert 180F to C convert 356F to F convert 30" to cm etc checking key: (?i-xsm:(\d+)F) Matched F with 180 Matched F with 356 convert 82C to C convert 180C to F convert 30" to cm etc convert 82C to C convert 180C to F convert 30" to cm etc
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Global replace issue
by moritz (Cardinal) on Aug 29, 2010 at 18:44 UTC | |
|
Re: Global replace issue
by FunkyMonk (Bishop) on Aug 29, 2010 at 18:39 UTC | |
|
Re: Global replace issue
by johngg (Canon) on Aug 29, 2010 at 22:23 UTC | |
|
Re: Global replace issue
by mr_mischief (Monsignor) on Aug 30, 2010 at 11:03 UTC | |
by aquarium (Curate) on Aug 30, 2010 at 23:14 UTC | |
|
Re: Global replace issue
by aquarium (Curate) on Aug 29, 2010 at 23:55 UTC |