On 5 Mar 2003, jdavis wrote:
> hello,
> I am trying to test a key from a hash in a hash
> the script gives me the desired result overall but i get
> the warning ...
> Use of uninitalized value in pattern match (m//) at ./passwd.pl line 26.
> could someone tell me why i get this error
>
> here is the code (this scrip looks at /etc/passwd and gets info
> from the desired field on a user - user and field args are passed on
> command line...
>
>
> #! /usr/bin/perl -w
> ## fun with hash -- no pipe needed :)
>
Throw in a use strict too :-)
> $who = $ARGV[0];
> $what = $ARGV[1];
>
> unless($who && $what ){
> print "Usage - passwd.pl username field\n";
> print "Ex. passwd.pl root uid\n";
> exit;
> }
>
> $file = "/etc/passwd";
> open(PASS, "$file") || die "cant open file";
> @array = <PASS>;
> close(PASS);
You already have perl functions that do this for you
perldoc -f getpwnam
>
>
> for $i (@array){
> @_ = split(/\:/, $i);
You don't have to escape ':'
You might want to store the result of split in a different array. @_ is
also the array that contains the arguments to a subroutine. This can be
confusing.
> @fields = qw/user pass uid gid special home shell/;
> $name = $_[0];
> foreach $f (@fields){
> $hoh{"$name"}{$f} = shift(@_);
> }
> }
You are reading in the entire /etc/passwd file and finally looking for
only one entry. You can filter based on the username in the above for loop
itself.
>
> ## added \w test because roots uid is 0 therefor making tests false
> if($hoh{$who}{$what} =~ /\w/){
> print "$hoh{$who}{$what}\n";
> }
You might want to check if the key $who is present in %hoh before
accessing it. Use Data::Dumper to view the hash you have formed.
perldoc Data::Dumper;
>
> else{
> print "The combo $who and $what doesnt compute\n";
> print "Try again - Bye\n";
> }
>
>
> thanks,
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]