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

Anyway to shorten this so I dont have to use the variable "$field" each time?? Maybe I could just use the variable just once? If so how??
next if (($field eq 'first') || ($field eq 'Second') || ($field eq 'th +ird'));

Replies are listed 'Best First'.
Re: Fine tuning my condition statement
by Aristotle (Chancellor) on Nov 20, 2002 at 18:54 UTC

    Regex alternation is costly and somewhat crufty.

    Building a hash works, but I'd prefer to at least do it non-iteratively:

    my %stop; @stop{qw(first second third)} = (); next if exists $stop{$field};
    Personally, I'd use the following: next if grep $field eq $_, qw(first second third); grep in scalar context returns the number of matches.

    Makeshifts last the longest.

Re: Fine tuning my condition statement
by strider corinth (Friar) on Nov 20, 2002 at 18:28 UTC
    One way would be this:
    next if $field =~ /^(first|second|third)$/;
    The '^' matches the beginning of a string, and the '$' matches the end, emulating an 'eq' function for whatever's in between them. The '|'s are roughly equivalent to an 'or' statement: this will match 'first' or 'second' or 'third'.

    Update: added explanation, and parens, thanks to Enlil.
    --
    Love justice; desire mercy.
      Thanks for everyones help and answers.
Re: Fine tuning my condition statement
by fruiture (Curate) on Nov 20, 2002 at 18:29 UTC

    There's no real reason to shorten this if it will stay that way. Shorter is not always better.

    You could make it more scalable like that:

    next if exists { map {$_=>undef} qw(first second third) }->{ $field }

    But that is certainly less effivient than hard-coded! Use this (or something like it) only if you need this point to be that extendable.

    --
    http://fruiture.de
Re: Fine tuning my condition statement
by BrowserUk (Patriarch) on Nov 20, 2002 at 19:13 UTC

    Or even (though some might consider it hookey :^)

    next if 1+index( 'first Second third', $field );

    With the caveat that if $field can contain spaces, you need to use a different delimeter, and pick one that cannot be a part of $field. "\cA" is a favorite of mine.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Fine tuning my condition statement
by nothingmuch (Priest) on Nov 20, 2002 at 18:57 UTC
    You could also do something like:
    $field eq $_ and next for (qw/first Second third/);
    But I suspect It'll have a bit of an overhead... This saves the typing, but avoids a regex if you don't want to get into that.

    -nuffin
    zz zZ Z Z #!perl