On 7/26/07, snickwad <[EMAIL PROTECTED]> wrote:
Apologies if this is a bit noobie, but I have been left with a short
window to write a tool and I have had no experience with Perl. Very
familiar with Kornshell, and so far my Perl script is going okay.
However, I am struggling to get a test working correctly. Is there a
simple way to test if a string does not exist in an array?

I am trying to test if some user input (a userid) is valid and error
if it does not exist. Currently have the contents of /etc/passwd in an
array, and can test for valid users, but all my efforts to test for
invalid have failed miserably.

I am on AIX, not sure on the Perl version.

Current test for valid is :

return 1 if (grep(/$user/,$udata));

Any help would be much appreciated.

Ashley

You don't have any arrays in your example.  Try this:

[EMAIL PROTECTED]:~$ cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

my $user = shift;

open my $f, "<", "/etc/passwd"
       or die "could not open /etc/passwd:$!";
my @users = map { (split ':')[0] } <$f>;

print "$user is ";
if (grep { $user eq $_ } @users) {
       print "valid\n";
} else {
       print "invalid\n";
}

[EMAIL PROTECTED]:~$ perl t.pl cowens
cowens is valid
[EMAIL PROTECTED]:~$ perl t.pl cowens1234
cowens1234 is invalid

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to