[tpm] I love this list! (was: Re: Regex question - Solved)

Fernando Oliveira fernandocorrea at gmail.com
Tue Jul 8 09:38:06 PDT 2008


$bar="$2 - $1" if $foo=~/(\w+-\d+)-(\d+)/;
is the best way...

2008/7/8 Madison Kelly <linux at alteeve.com>:

>  So many people replied so quickly, thank you all!
>
>  I should have been more clear; '$foo' is set further up; I set in manually
> for this email only.
>
>  I've got this working (real-world where I want two variables):
>
> my ($bar, $baz)=$foo=~/(\w+-\d+)-(\d+)/;
>
>  May I extend the question?
>
>  How would I go about working with '$1', '$2', etc if I wanted to do
> something odd, like say (totally fabricated, broken example):
>
> my $bar="$2 - $1"=$foo=~/(\w+-\d+)-(\d+)/;
>
>  Again, thanks all. This list rocks, if only because of the enthusiasm
> shown for code optimization (of course, for more reasons, too!).
>
> Madi
>
> Madison Kelly wrote:
>
>> Hi all,
>>
>>  I've got a simple problem I often come across, and I've got a way to make
>> it work. However, I've always felt there must be a more ... elegant way of
>> doing it.
>>
>> For example, let's say I want to copy a variable and strip a bit off the
>> end;
>>
>> 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? Specifically,
>> is there a way to assign '$1' to a new variable in one go?
>>
>> Thanks!
>>
>> Madi
>>
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>



-- 
Just another Perl Hacker,
Fernando (SmokeMachine)
http://perl-e.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20080708/ed34fa3e/attachment.html>


More information about the toronto-pm mailing list