Lhamo_rin has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way I could combine and/or shorten the below code?
for (@info) { s/i_//; } for (@data) { s/d_//;
Lhamo_rin

Replies are listed 'Best First'.
Re: Shorter Code
by davorg (Chancellor) on Jul 02, 2003 at 12:15 UTC

    Not really much shorter but...

    s/i_// for @info; s/d_// for @data;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Shorter Code
by Zaxo (Archbishop) on Jul 02, 2003 at 12:19 UTC

    I don't see any logical reason to combine them, but they can be shortened with the modifier form of the loop:

    s/i_// for @info; s/d_// for @data;
    If such a substitution is a common operation, you could make a subroutine that modifies its operands:
    # Usage: erase STRING, LIST sub erase { my $re = quotemeta shift; s/$re//g for @_; 1; }

    After Compline,
    Zaxo

Re: Shorter Code
by Joost (Canon) on Jul 02, 2003 at 12:23 UTC
    If @info does not contain "d_" and @data does not contain "i_" you can do:
    s/d_|i_//for@info,@data;
    Joost.
    -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker\
Re: Shorter Code
by Abstraction (Friar) on Jul 02, 2003 at 12:19 UTC
    s/i_// for @info;

    and

    s/d_// for @data;

Re: Shorter Code
by rir (Vicar) on Jul 02, 2003 at 13:53 UTC
    I read this and first worry that the code is too short.

    Should those regexes be anchored?    s/^i_// instead of   s/i_//.

    --
    rir
    Live the dream.

Re: Shorter Code
by tilly (Archbishop) on Jul 02, 2003 at 17:43 UTC
    Why is shortening it your immediate goal? See The path to mastery for my thoughts on why it shouldn't be.