> Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
> Date: Wed, 4 Dec 2002 18:26:17 -0800
> From: Michael G Schwern <[EMAIL PROTECTED]>
> Content-Disposition: inline
> Sender: Michael G Schwern <[EMAIL PROTECTED]>
> X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/
> 
> (The post about 'purge' just made me remember this idea)
> 
> Lets say you have a list of program arguments.
> 
>     @ARGV = ('foo', '--bar=baz', 'yar');
> 
> and you want to seperate that into two lists.  One of switches and one of
> normal args.  You can't just use a grep, you'd have to do this:
> 
>     my @switches = ();
>     my @args = ();
>     foreach (@ARGV) {
>         if( /^-/ ) {
>           push @switches, $_;
>       }
>       else {
>           push @args, $_;
>       }
>     }

Or the concise, kindof-unreadable way:

    push (/^-/ ?? @switches :: @args), $_ for @*ARGS;

It's too bad Perl5 croaks in so many different ways for the equivalent
(the extra parens around push's arglist and the fact that /^-/ ? @s :
@a isn't an array :( )

About your idea, though, I'm rather indifferent.  However, a friend of
mine once asked me if Perl had "search" or "find" operation, returning
the I<index> of matching elements.  Now am I just being braindead, or
is Perl actually missing this operation?  Do you really have to:

    my $index;
    for @a {
        last if la_dee_daa;
        $index++;
    }

That can't be right....

But if it is, perhaps a C<find> function alongside C<map> and C<grep>
would do.  This is all premature---we have to wait for A28 before we
start suggesting any of these.  But we'll be ready for 2012! ;)

Luke

Reply via email to