[tpm] Regex question

Indy Singh indy at indigostar.com
Tue Jul 8 08:50:00 PDT 2008


Use this code:
1: my $foo="ABC-987-01";
2: my ($bar) = $foo =~ /(\w+-\d+)-\d+/;
3: # $bar is now 'ABC-987'.
4: print "$bar\n";

Note that on line 2, the left side of the assignment is in list context
because $bar is enclosed in brackets.
my $bar = ... will not work because it is n scalar context.

Indy Singh
IndigoSTAR Software -- www.indigostar.com


----- Original Message ----- 
From: "Madison Kelly" <linux at alteeve.com>
To: "Toronto Perl Mongers" <tpm at to.pm.org>
Sent: Tuesday, July 08, 2008 11:00 AM
Subject: [tpm] Regex question


> 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 



More information about the toronto-pm mailing list