<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Using Scalar Variables in Regular Expressions</title>
	<atom:link href="http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/</link>
	<description>Bay View Consulting Services, Inc.</description>
	<pubDate>Thu, 28 Aug 2008 18:15:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-41019</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Thu, 26 Jun 2008 00:16:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-41019</guid>
		<description>The contents of your variable $sepchar contain a regular expression metacharacter, so that's like saying split(/&#124;/, $buffer) without the backslash.  To add the backslash use the \Q escape code, like this:

@row = split(/\Q$sepchar\E/, $buffer);

You can think of \Q as similar to \U which converts letters to upper case; however what \Q does is insert a \ before any non-alphanumeric characters.  The \E indicates the end of the quoted portion, in this case it would be optional since it's at the end.</description>
		<content:encoded><![CDATA[<p>The contents of your variable $sepchar contain a regular expression metacharacter, so that&#8217;s like saying split(/|/, $buffer) without the backslash.  To add the backslash use the \Q escape code, like this:</p>
<p>@row = split(/\Q$sepchar\E/, $buffer);</p>
<p>You can think of \Q as similar to \U which converts letters to upper case; however what \Q does is insert a \ before any non-alphanumeric characters.  The \E indicates the end of the quoted portion, in this case it would be optional since it&#8217;s at the end.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: george</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-41014</link>
		<dc:creator>george</dc:creator>
		<pubDate>Wed, 25 Jun 2008 20:48:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-41014</guid>
		<description>Hi, I have this portion of code:

my $sepchar = "&#124;"; # pipe symbol
@row = split(/$sepchar/, $buffer);

However, this doesn't work, but

@row = split(/\&#124;/, $buffer)

DOES work. How can I mask special chars in the variable to determine the sepchar dynamically?
Thanks for any help.</description>
		<content:encoded><![CDATA[<p>Hi, I have this portion of code:</p>
<p>my $sepchar = &#8220;|&#8221;; # pipe symbol<br />
@row = split(/$sepchar/, $buffer);</p>
<p>However, this doesn&#8217;t work, but</p>
<p>@row = split(/\|/, $buffer)</p>
<p>DOES work. How can I mask special chars in the variable to determine the sepchar dynamically?<br />
Thanks for any help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-40763</link>
		<dc:creator>James</dc:creator>
		<pubDate>Fri, 20 Jun 2008 16:49:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-40763</guid>
		<description>Joe,
You can actually do this quite simply with the map function.  However, I really doubt this will be any faster.

my @array = (...);
@array = map {$_ =~ s/\r//g} @array;

You've probably figured this out by now, but it may be useful for other people stopping by.</description>
		<content:encoded><![CDATA[<p>Joe,<br />
You can actually do this quite simply with the map function.  However, I really doubt this will be any faster.</p>
<p>my @array = (&#8230;);<br />
@array = map {$_ =~ s/\r//g} @array;</p>
<p>You&#8217;ve probably figured this out by now, but it may be useful for other people stopping by.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-29678</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Thu, 28 Feb 2008 02:41:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-29678</guid>
		<description>I don't have any background in tcl, so I'm not spoiled the way you are :)

I'm pretty sure Perl doesn't have a way to do what you're asking for - only various forms of loops (foreach, grep, or map).  In certain situations you could join the array elements into a string and apply your regex on that, I suppose.

You might want to ask your question on perlmonks.org.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t have any background in tcl, so I&#8217;m not spoiled the way you are :)</p>
<p>I&#8217;m pretty sure Perl doesn&#8217;t have a way to do what you&#8217;re asking for - only various forms of loops (foreach, grep, or map).  In certain situations you could join the array elements into a string and apply your regex on that, I suppose.</p>
<p>You might want to ask your question on perlmonks.org.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-29618</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Wed, 27 Feb 2008 10:34:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-29618</guid>
		<description>I realise this is an old posting but i came here via a google search regarding scalar vars and regexp, coming from a tcl enviroment i have been looking for a way to apply a regexp substitution to an array as a whole vs looping through each item in perl. In terms of performance tcl blows it away in this context, im for certain though that if i could apply the regexp to the entire array in one iteration it would be the opposite result. The question is, how is it possible?

because it wont let you do

@array =~ s/\r//g;

versus 

foreach $item @array { $item =~ s/\r//g; }

the ladder takes a substantially longer time with 100,000 lines of text than its tcl equivlant.</description>
		<content:encoded><![CDATA[<p>I realise this is an old posting but i came here via a google search regarding scalar vars and regexp, coming from a tcl enviroment i have been looking for a way to apply a regexp substitution to an array as a whole vs looping through each item in perl. In terms of performance tcl blows it away in this context, im for certain though that if i could apply the regexp to the entire array in one iteration it would be the opposite result. The question is, how is it possible?</p>
<p>because it wont let you do</p>
<p>@array =~ s/\r//g;</p>
<p>versus </p>
<p>foreach $item @array { $item =~ s/\r//g; }</p>
<p>the ladder takes a substantially longer time with 100,000 lines of text than its tcl equivlant.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-1200</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Sat, 16 Sep 2006 05:59:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-1200</guid>
		<description>Yes, of course you're right.  I wonder how I missed that!  Thanks.  I edited the post with a line through the mistake.</description>
		<content:encoded><![CDATA[<p>Yes, of course you&#8217;re right.  I wonder how I missed that!  Thanks.  I edited the post with a line through the mistake.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bla</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-1084</link>
		<dc:creator>bla</dc:creator>
		<pubDate>Sun, 27 Aug 2006 18:28:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-1084</guid>
		<description>this should say "Single-quoted strings are used when setting $regex to avoid needing an extra backslash." not "Double-quoted strings are used when setting $regex to avoid needing an extra backslash."</description>
		<content:encoded><![CDATA[<p>this should say &#8220;Single-quoted strings are used when setting $regex to avoid needing an extra backslash.&#8221; not &#8220;Double-quoted strings are used when setting $regex to avoid needing an extra backslash.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William Ward</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-8</link>
		<dc:creator>William Ward</dc:creator>
		<pubDate>Mon, 30 Jan 2006 10:11:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-8</guid>
		<description>Excellent point.  However there are a couple of minor problems with your code:

You used = instead of =&#62; in the "use constant" lines
You need to add a backslash (\) before the constant names in order to make a scalar reference that can be dereferenced.

Here's the corrected code:

use constant NORNAL_REQ =&#62; 1;
use constant ADD_REQ =&#62; 2;
use constant FAILED_REQ =&#62; 3;


my $test = 2;
SWITCH: for($test) {
/^${\NORMAL_REQ}$/x &#38;&#38; do { print "case 1\n"; last SWITCH;};
/^${\ADD_REQ}$/x &#38;&#38; do { print "case 2\n"; last SWITCH;};
/^${\FAILED_REQ}$/x &#38;&#38; do { print "case 3\n"; last SWITCH;}
}
</description>
		<content:encoded><![CDATA[<p>Excellent point.  However there are a couple of minor problems with your code:</p>
<p>You used = instead of =&gt; in the &#8220;use constant&#8221; lines<br />
You need to add a backslash (\) before the constant names in order to make a scalar reference that can be dereferenced.</p>
<p>Here&#8217;s the corrected code:</p>
<p>use constant NORNAL_REQ =&gt; 1;<br />
use constant ADD_REQ =&gt; 2;<br />
use constant FAILED_REQ =&gt; 3;</p>
<p>my $test = 2;<br />
SWITCH: for($test) {<br />
/^${\NORMAL_REQ}$/x &amp;&amp; do { print &#8220;case 1\n&#8221;; last SWITCH;};<br />
/^${\ADD_REQ}$/x &amp;&amp; do { print &#8220;case 2\n&#8221;; last SWITCH;};<br />
/^${\FAILED_REQ}$/x &amp;&amp; do { print &#8220;case 3\n&#8221;; last SWITCH;}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://www.bayview.com/blog/2004/08/04/using-scalar-variables-in-regular-expressions/#comment-7</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Mon, 30 Jan 2006 09:01:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.bayview.com/blog/?p=20#comment-7</guid>
		<description>There is a more interesting case: using constant in the regexp. For example, I have constants:
&lt;code&gt;
use constant NORNAL_REQ = 1;
use constant ADD_REQ = 2;
use constant FAILED_REQ = 3;
&lt;/code&gt;
And then I'd like to use them in the one of  representation of SWITCH block:
&lt;code&gt;
my $test = 2;
SWITCH: for($test) {
    /^${NORMAL_REQ}$/x &#38;&#38; do { do something for case 1 … ; last SWITCH;};
    /^${ADD_REQ}$/x &#38;&#38; do { do something for case 2 … ; last SWITCH;};
    /^${FAILED_REQ}$/x &#38;&#38; do { do something for case 3 … ; last SWITCH;}
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>There is a more interesting case: using constant in the regexp. For example, I have constants:<br />
<code><br />
use constant NORNAL_REQ = 1;<br />
use constant ADD_REQ = 2;<br />
use constant FAILED_REQ = 3;<br />
</code><br />
And then I&#8217;d like to use them in the one of  representation of SWITCH block:<br />
<code><br />
my $test = 2;<br />
SWITCH: for($test) {<br />
    /^${NORMAL_REQ}$/x &amp;&amp; do { do something for case 1 … ; last SWITCH;};<br />
    /^${ADD_REQ}$/x &amp;&amp; do { do something for case 2 … ; last SWITCH;};<br />
    /^${FAILED_REQ}$/x &amp;&amp; do { do something for case 3 … ; last SWITCH;}<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>
