[tpm] Regex question

Indy Singh indy at indigostar.com
Wed Jul 9 09:27:35 PDT 2008


Alan,
If you are suggesting that it is better to have left it as the original 
multi line code instead of the compressed version,  I am not sure that I 
agree with that.

Here is an example using different variable names to clarify the intent:
First written your way:
1: $name = "Alan Rocker";
2: $first_name = $name;
3: $first_name =~ s/\s.*$//;        # remove the last name

The problem I have with this code is that if you look at line 2, it is 
temporarily wrong.  At this point $first_name contains the full name so 
it is wrong.  Then we correct the error on line 3.  Since I read code 
one line at a time, my brain throws an exception when I see line 2.  It 
is possible to somewhat clarify the intent my merging line 2 and 3:

$first_name = $name; $first_name =~ s/\s.*$//;

But I really prefer the following method:
1: $full_name = "Alan Rocker";
2: ($first_name) = $full_name =~ /(*?)\s/;

In this example at any point the varaibles only ever contain what they 
are supposed to contain, i.e.  $first_name actually contains the first 
name.

To further expand the example to include a common need to split fields 
from a line:
1: $full_name = "Alan Rocker";
2: ($first_name, $last_name) = $full_name =~ /(*?)\s(.*)/;

(Note untested code, may contain bugs)

Now, where did I put my accordian? ...

Indy Singh
IndigoSTAR Software -- www.indigostar.com


----- Original Message ----- 
From: <arocker at vex.net>
To: "Toronto Perl Mongers" <tpm at to.pm.org>
Sent: Wednesday, July 09, 2008 11:51 AM
Subject: Re: [tpm] Regex question


> Madi's original question, and one typical response:
>
>>> my $foo="ABC-987-01";
>>> my $bar=$foo;
>>> $bar=~s/(\w+-\d+)-\d+/$1/;
>>> # $bar now 'ABC-987'.
>>>
>>> That's three lines. Is there a way to do this in one line?
>>
>> Maybe I misunderstood the question...  You can say
>>
>>    my ($bar) = $foo =~ /(\w+-\d+)-\d+/;
>>
>> which will assing $1 to $bar if the match succeeds.
>>
> Compressing the assignment (which really doesn't include the original
> setting of $foo) saves one line ad one naming of $bar at the expense 
> of
> complexity and inflexibility.
>
> To comprehend the expression, you have to keep an extra level on the
> mental stack, and if you want to change the destination you risk 
> changing
> the regex. (The two expressions aren't completely equivalent; the 
> original
> doesn't change $foo, the other, like most of the suggestions, does.)
>
> Sometimes, it pays to remember the KISS principle, and the joke (?) 
> about
> a gentleman being a man who knows how to play the accordion, but 
> doesn't.
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm 



More information about the toronto-pm mailing list