Re: Can this OR be shortened?

2017-03-25 Thread Brandon Allbery
On Fri, Mar 24, 2017 at 10:45 PM, Brad Gilbert  wrote:

> Basically use | in regexes unless you need ||.


There's been some discussion, here and in IRC, of the | form interacting in
ways that are correct but which people often don't expect. This can result
in match failures or unexpected slowness. My takeaway is there's no
simple-minded "do this" or "do this if/unless" rule as yet.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Can this OR be shortened?

2017-03-25 Thread Timo Paulssen
On 25/03/17 12:53, Tom Browder wrote:
> On Fri, Mar 24, 2017 at 10:36 PM, Timo Paulssen  wrote:
>> I seem to recall you asked about performance recently
>>
>> the regex engine has a significant overhead.
> ...
>
> Isn't that somewhat mitigated by defining regex or token constants for reuse?
>
> -Tom

The overhead is from starting up and finishing a regex match. Creating
the Match object is quite expensive, for example. all regexes are
compiled into code objects, of course.


Re: Can this OR be shortened?

2017-03-25 Thread yary
On Sat, Mar 25, 2017 at 1:15 AM, ToddAndMargo  wrote:
> On 03/24/2017 07:45 PM, Brad Gilbert wrote:
>>
>> All of these should work
>>
>> if $Terminal ~~ /xterm/ | /linux/ {}
>> if $Terminal ~~ /xterm | linux/ {}
>> if $Terminal ~~ /xterm || linux/ {}
>>
>> Note that | in a regex tries both sides as if in parallel, and goes
>> for the longest,
>
>
> Hi Brad,
>
> What do you mean by longest?


my $a = 'Bart sez cowabunga!';

# matches cowabunga because that's the longest match
$a ~~ / cow | cowabunga /;
say $/;  # "cowabunga"

# matches cow because it is the first alternation
$a ~~ / cow || cowabunga /;
say $/;  # "cow"

This is useful in writing parsers- when given a choice of keywords
that have the same prefix, longest match will find the keyword people
expect regardless of the order of the alternations. Keeps people from
having to remember to also search for a word boundary at the end,
makes it easier to write bug-free grammars.

-y


Re: Can this OR be shortened?

2017-03-24 Thread Timo Paulssen
I seem to recall you asked about performance recently

the regex engine has a significant overhead. Your regex is equivalent to

$Terminal.contains('xterm' | 'linux')

though of course if you only test this once at the beginning of the
program, you can ignore that.


Re: Can this OR be shortened?

2017-03-24 Thread Darren Duncan
Just speculating, but try replacing the "||" with the "|" operator which should 
create an ANY Junction, if I'm not mistaken, which may then do what you want. -- 
Darren Duncan


On 2017-03-24 5:58 PM, ToddAndMargo wrote:

Hi All,,

if $Terminal ~~ /xterm/ || /linux/ {}
does not work

But this does
if $Terminal ~~ /xterm/ || $Terminal ~~ /linux/ {}

Can the if statement be shortened such that I do not
have to repeat $Terminal?


Many thanks,
-T



Re: Can this OR be shortened?

2017-03-24 Thread ToddAndMargo

On 03/24/2017 06:14 PM, Brandon Allbery wrote:


On Fri, Mar 24, 2017 at 8:58 PM, ToddAndMargo > wrote:

if $Terminal ~~ /xterm/ || /linux/ {}


if $Terminal ~~ /xterm || linux/ {}


Perfect!  Thank you!  I keep forgetting spaces are ignored in regex's


Re: Can this OR be shortened?

2017-03-24 Thread Brandon Allbery
On Fri, Mar 24, 2017 at 8:58 PM, ToddAndMargo  wrote:

> if $Terminal ~~ /xterm/ || /linux/ {}


if $Terminal ~~ /xterm || linux/ {}


-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Can this OR be shortened?

2017-03-24 Thread ToddAndMargo

Hi All,,

if $Terminal ~~ /xterm/ || /linux/ {}
does not work

But this does
if $Terminal ~~ /xterm/ || $Terminal ~~ /linux/ {}

Can the if statement be shortened such that I do not
have to repeat $Terminal?


Many thanks,
-T

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~