Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Text switching

by Fletch (Bishop)
on Sep 14, 2022 at 07:01 UTC ( [id://11146874]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Text switching
in thread Text switching

DERP, good point; for some reason I read that as having the matched number in it. Handwaving attempt below using /e and adding a bit of logic to the RHS of the substitution would work. Still has the problem of the fuzziness that it's non-trivial to be sure you're only pulling apartment numbers with just the regex and not getting years or what not.

A better solution would be to annotate explicitly in the source text as mentioned elsewhere. Better still use a real templating solution to explicitly mark up things. (e.g. TT like See more about [% apt_link( 1002 ) %] )

#!/usr/bin/env perl use 5.034; my $text = <<'EOT'; This is an appt: 1203 So is 1001. But 8675 is not. EOT chomp($text); my $digits_4_re = qr{ (?<!-)\b[0-9]{4}\b(?!-) }xms; my %valid_appt_numbers = ( 1001 => 1, 1203 => 1, ); sub _anchor_if_apt { my $candidate = shift; if ( exists $valid_appt_numbers{$candidate} ) { return _anchor_for_num($candidate); } else { return $candidate; } } sub _anchor_for_num { my $unit = shift; return qq{<a href="apartment.pl?do_what=view&unit=$unit"><b>$unit< +/b></a>}; } $text =~ s{($digits_4_re)}{ _anchor_if_apt( $1 ) }xsmeg; say $text; exit 0; __END__ $ perl texty.plx This is an appt: <a href="apartment.pl?do_what=view&unit=1203"><b>1203 +</b></a> So is <a href="apartment.pl?do_what=view&unit=1001"><b>1001</b></a>. But 8675 is not.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: Text switching
by AnomalousMonk (Archbishop) on Sep 14, 2022 at 08:33 UTC
    A better solution would be to annotate explicitly in the source text [or] use a real templating solution ...

    I strongly agree. Just too much ambiguity about whether 2023 is a unit number, year, part of a phone number or address, etc., etc. And then there are the unit number variations that will surely be introduced sooner or later.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146874]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found