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

Gentle_monks,
I realize one would think I knew enough now to see my error but I don't. I need to restructure this:
for $admin_user (@admin_users) { if ($author =~ /($admin_user)/i) { print "$blah"; } else { print "$uber_blah "; } }

so that it goes through @admin_user and determines if $author is a match for any of @admin_user 's values and then executes the conditional once. The way it goes now it executes once for each value in @admin_user.
Any advice would be much appreciated!
TIA
jg
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ

Replies are listed 'Best First'.
Re: Is for the wrong choice here?
by Masem (Monsignor) on Jan 18, 2002 at 21:57 UTC
    grep is better here:
    if ( grep { /($author)/i } @admin_users ) { print "$blah\n"; } else { print "$uber_blah\n"; }

    Update -- Should be matching on $author, not $admin_user (the value from the loop in question).

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      This should work a little better
      #!/usr/bin/perl -w use strict; my $author = 'grep'; my @admin_users = qw/test that grep out/; if ( grep { /^$author$/i } @admin_users ) { print "blah\n"; } else { print "uber_blah\n"; }


      grep
      grep> cd pub
      grep> more beer
      Well, that works better than what I had except that it apparently tests true every time. Every iteration is printing $blah even when $author is not a match. Advice?
      TIA
      jg
      _____________________________________________________
      If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
        This code gets the same results:
        if ( grep { /($admin_user)/i } $author ) { print "$blah "; } else { print "$uber_blah"; }

        TIA
        jg
        _____________________________________________________
        If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
Re: Is for the wrong choice here?
by trs80 (Priest) on Jan 18, 2002 at 22:46 UTC
    If you simply want to stop the loop once it has matched
    use last
    for $admin_user (@admin_users) { if ($author =~ /($admin_user)/i) { print "$blah"; last; } else { print "$uber_blah "; } }
Re: Is for the wrong choice here?
by tadman (Prior) on Jan 18, 2002 at 22:47 UTC
    It may help to define what you are trying to do better, before hunting for errors in code. Perhaps your approach is sound, but not in line with your functional expectations.

    Interpretation: You are trying to find out if $author is in the @admin_users list, and return true/false accordingly.
    if (grep { lc eq lc($author) } @admin_users) { print "$blah"; } else { print "$uber_blah"; }
    Of course, I would make sure that you prune your @admin_users list, making it all neat and tidy before feeding it to this routine. For example, you will want to strip off extraneous characters, such as dangling spaces and newlines.
    @admin_users = map { s/\s+//; $_ } @admin_users;
Re: Is for the wrong choice here?
by daviddd (Acolyte) on Jan 19, 2002 at 02:56 UTC
    Based on the advice in perlfaq4 (see the section "How can I tell whether a list or array contains a certain element?"), I use a hash for this sort of thing. e.g.:
    my %admin_users = (
    John => 1,
    Bill => 1,
    Susie => 1,
    );
    
    Then it's easy to test:
    if ( $admin_users{$author} )
    {
       print $blah;
    }
    else
    {
       print $uber_blah;
    }
    
Re: Is for the wrong choice here?
by jlongino (Parson) on Jan 18, 2002 at 23:33 UTC
    I suspect that the contents of one or more of your variables is not what you think. Try the following untested code (or the corrected semblance thereof):
    print "'$_'\n" foreach @admin_users; print "\n\n\$author: '$author'\n"; print "\$blah: $blah\n"; print "\$uber_blah: $uber_blah\n";
    I notice that blah and uber_blah are variables. Do they contain different values? HTH.

    --Jim

Re: Is for the wrong choice here?
by jerrygarciuh (Curate) on Jan 19, 2002 at 02:11 UTC
    Thanks to all who replied. I kept running into testing all true or else all false and finally gave up on keeping the admins as an array. The code below tests true for all names in $admin_users and no other names. This may be crap programming but it works!
    TIA
    jg
    my $admin_users = "Jason|WAYNE|Rob"; if ($author =~ /($admin_users)/i) { print "$blah"; } else { print "$uber_blah"; }
    _____________________________________________________
    If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
      This does not do quite what you want.

       $author = 'John_Wayne_Bobbit';
      This will evaluate to true, but according your $admin you do not want any porn stars accessing your program.

      If you follow my post above and use an array of names you will be fine.

      grep
      grep> cd pub
      grep> more beer