short-hand for same regex for multiple scalars?

Discussion in 'Perl Misc' started by Alexander Huber, Jun 28, 2006.

  1. 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
    mailto:
    Alexander Huber, Jun 28, 2006
    #1
    1. Advertising

  2. Alexander Huber

    Paul Lalli Guest

    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
    Paul Lalli, Jun 28, 2006
    #2
    1. Advertising

  3. 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
    mailto:
    Alexander Huber, Jun 28, 2006
    #3
  4. 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
    John W. Krahn, Jun 28, 2006
    #4
  5. 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;
    Brian McCauley, Jun 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. redguardtoo
    Replies:
    0
    Views:
    305
    redguardtoo
    Jul 26, 2004
  2. Roedy Green

    short hand catch

    Roedy Green, Sep 8, 2003, in forum: Java
    Replies:
    2
    Views:
    390
    Brad BARCLAY
    Sep 8, 2003
  3. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    544
    Keith Thompson
    Jan 11, 2007
  4. Replies:
    4
    Views:
    798
    Kaz Kylheku
    Oct 17, 2006
  5. redguardtoo
    Replies:
    0
    Views:
    92
    redguardtoo
    Jul 26, 2004
Loading...

Share This Page