Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Problem #1</h2> <p>As there doesn't appear to be much structure shared by the individual regexes, there's not really a simpler or clearer way than just listing the commands as you have done. One common approach to decreasing repetition in code like this is to move <code>$text</code> into <code>$_</code>, so that instead of having to say:</p> <pre><code>$text =~ s/foo/bar/g; </code></pre> <p>You can just say:</p> <pre><code>s/foo/bar/g; </code></pre> <p>A common idiom for doing this is to use a degenerate <code>for()</code> loop as a topicalizer:</p> <pre><code>for($text) { s/foo/bar/g; s/qux/meh/g; ... } </code></pre> <p>The scope of this block will preserve any preexisting value of <code>$_</code>, so there's no need to explicitly <code>local</code>ize <code>$_</code>.</p> <p>At this point, you've eliminated almost every non-boilerplate character -- how much shorter can it get, even in theory?</p> <p>Unless what you really want (as your problem #2 suggests) is improved <em>modularity</em>, e.g., the ability to iterate over, report on, count etc. all regexes.</p> <h2>Problem #2</h2> <p>You can use the <code>qr//</code> syntax to quote the "search" part of the substitution:</p> <pre><code>my $search = qr/(&lt;[^&gt;]+&gt;)/; $str =~ s/$search/foo,$1,bar/; </code></pre> <p>However I don't know of a way of quoting the "replacement" part adequately. I had hoped that <code>qr//</code> would work for this too, but it doesn't. There are two alternatives worth considering:</p> <p><strong>1. Use <code>eval()</code> in your <code>foreach</code> loop.</strong> This would enable you to keep your current <code>%rxcheck2</code> hash. Downside: you should always be concerned about safety with string <code>eval()</code>s.</p> <p><strong>2. Use an array of anonymous subroutines:</strong></p> <pre><code>my @replacements = ( sub { $_[0] =~ s/&lt;[^&gt;]+&gt;/ /g; }, sub { $_[0] =~ s/\s+/ /g; }, sub { $_[0] =~ s/[\(\{\[]\d+[\(\{\[]/ /g; }, sub { $_[0] =~ s/\s+[&lt;&gt;]+\s+/\. /g }, sub { $_[0] =~ s/\s+/ /g; }, sub { $_[0] =~ s/\.*\s*[\*|\#]+\s*([A-Z\"])/\. $1/g; }, sub { $_[0] =~ s/\.\s*\([^\)]*\) ([A-Z])/\. $1/g; } ); # Assume your data is in $_ foreach my $repl (@replacements) { &amp;{$repl}($_); } </code></pre> <p>You could of course use a hash instead with some more useful key as the hash, and/or you could use multivalued elements (or hash values) including comments or other information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload