Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > short-hand for same regex for multiple scalars?

Reply
Thread Tools

short-hand for same regex for multiple scalars?

 
 
Alexander Huber
Guest
Posts: n/a
 
      06-28-2006
Hi,

Is there a shorter way of writing the following (regex only once on
right-hand side?), basically I want to apply the same regular expression
consecutively to multiple scalars:

$a =~ s/a/b/gs;
$b =~ s/a/b/gs;
$c =~ s/a/b/gs;

Thanks,

Alexander.
--
Alexander Huber
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-28-2006
Alexander Huber wrote:
> Is there a shorter way of writing the following (regex only once on
> right-hand side?), basically I want to apply the same regular expression
> consecutively to multiple scalars:
>
> $a =~ s/a/b/gs;
> $b =~ s/a/b/gs;
> $c =~ s/a/b/gs;
>


Why not just use a loop?

for my $var ($a, $b, $c) {
$var =~ s/a/b/gs;
}

Paul Lalli

 
Reply With Quote
 
 
 
 
Alexander Huber
Guest
Posts: n/a
 
      06-28-2006
Many thanks!

Alexander.


Paul Lalli wrote:
> Alexander Huber wrote:
>> Is there a shorter way of writing the following (regex only once on
>> right-hand side?), basically I want to apply the same regular expression
>> consecutively to multiple scalars:
>>
>> $a =~ s/a/b/gs;
>> $b =~ s/a/b/gs;
>> $c =~ s/a/b/gs;
>>

>
> Why not just use a loop?
>
> for my $var ($a, $b, $c) {
> $var =~ s/a/b/gs;
> }
>
> Paul Lalli
>



--
Alexander Huber
(E-Mail Removed)
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-28-2006
Paul Lalli wrote:
> Alexander Huber wrote:
>>Is there a shorter way of writing the following (regex only once on
>>right-hand side?), basically I want to apply the same regular expression
>>consecutively to multiple scalars:
>>
>>$a =~ s/a/b/gs;
>>$b =~ s/a/b/gs;
>>$c =~ s/a/b/gs;
>>

>
> Why not just use a loop?
>
> for my $var ($a, $b, $c) {
> $var =~ s/a/b/gs;
> }


Since the OP wants a shorter way:

s/a/b/g for $a, $b, $c;




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-29-2006

John W. Krahn wrote:

> > Alexander Huber wrote:
> >>Is there a shorter way of writing the following (regex only once on
> >>right-hand side?), basically I want to apply the same regular expression
> >>consecutively to multiple scalars:
> >>
> >>$a =~ s/a/b/gs;
> >>$b =~ s/a/b/gs;
> >>$c =~ s/a/b/gs;

>
> s/a/b/g for $a, $b, $c;


It should also be noted that the very fact that the OP wants to perform
idenitical processing on several scalar values means he should consider
putting them in a agregate data type rather than separately named
scalars.

s/a/b/g for values %some_hash;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Need same viewstate for same page across multiple servers =?Utf-8?B?UHVuaXNoZXI=?= ASP .Net 1 03-23-2006 03:54 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
write to the same file from multiple processes at the same time? gabor Python 19 05-31-2005 01:57 PM
Re: write to the same file from multiple processes at the same time? Jp Calderone Python 7 05-28-2005 06:47 PM



Advertisments