in reply to Help with strict

Just a comment that you can rewrite the @master assignment to improve readability and save keystrokes -
my @masters = qw/ John Paul George Ringo /;
You can use a list to store names and still making it efficient for searching elements in the list, without using a hash table -
use strict; my @masters = qw/ John Paul George Ringo /; sub is_master { my $who = shift; return scalar grep /^$who$/, @masters; } for (qw/ John George James /) { if (is_master($_)) { print "$_ is master\n" } else { print "$_ is not master\n" } }
And the output is -
John is master George is master James is not master

Replies are listed 'Best First'.
Re: Re: Help with strict
by sauoq (Abbot) on Oct 29, 2003 at 01:51 UTC
    return scalar grep /^$who$/, @masters;

    Why use a regular expression when you are looking for string equality? What would your code do if $who contained something like 'foo^bar', 'foo{2}', or 'foo[bar]' (all of which are legal IRC nicks?) I think the hash idea given by hardburn is better than a grep, but if you wanted to use grep you should probably do it something like this:

    sub is_master { scalar grep $_[0] eq $_, @masters }
    And if you wanted to be sure it behaved exactly like the original sub, always returning either a 0 or a 1:
    sub is_master { scalar grep $_[0] eq $_, @masters and 1 }

    -sauoq
    "My two cents aren't worth a dime.";