> -----Original Message----- > From: David vd Geer Inhuur tbv IPlib > [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, June 25, 2002 6:28 AM > To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; > [EMAIL PROTECTED] > Subject: RE: if-statement and grep in one go > > > > Hello, > > Thanks for the solution Bob. > Changed some stuff and have 2 questions open. > > my $line; > my (%u, %g); > open(FILE, "< ${dir}/user.perm") or print "Failed opening > file $!"; ## 1 > while ($line = <FILE>) { > ## 2 > if ($line =~ /^user:/) { > $u{$_} = 1 for split(/,/, substr($line, 6)); > ## 3 > } > if ($line =~ /^group:/) { > $g{$_} = 1 for split(/,/, substr($line, 7)); > ## 4 > } > } > close(FILE); > > print "Invalid login"; exit unless $u{$pwuser} || > $g{$group}; ## 5
Somehow, I posted code other than that which I tested. Sorry about that. For those that might be interested in such things, here is the exact code I tested: #!/usr/bin/perl use Data::Dumper; my (%user, $group); while (my $line = <DATA>) { if ($line =~ /^user:\s*(.*)$/) { $user{$_} = 1 for split(/\s*,\s*/, $1); } if ($line =~ /^group:\s*(.*)$/) { $group{$_} = 1 for split(/\s*,\s*/, $1); } } print Data::Dumper::Dumper(\%user, \%group); __DATA__ user: vdgeerd, tester group: none, descr: all, I cut and pasted the sample input data you gave and put it under a __DATA__ tag. This let me read it through the DATA file handle. Data::Dumper was used to make sure the resulting hashes came out to the structure I expected. > > 1) I don't like to die in my script as there are many files > to read. And if I > can't open the current file I just want to continue with > the rest of the files > So I always prefer printing. OK, no problem. > 2) while my $line (<FILE>) didn't work, just a notation error. My goof, sorry. > 3) I don't need to split on spaces. The script that fills the > user.perm is > designed to always : "user: vdgeerd,and,other ## Never mind. > BUT, What do you do with the <1> ?? Changing it to $1 > doesn't matter too. I don't do anything with it. It's the keys that matter, not the values. 1 is just a dummy value, since each key must have a corresponding value. > I keep empty places when doing a " foreach my $value(%u) { > print $value; } " > chomp %u; didn't work either. > 4) Same as 3 > 5) I changed the && into || cause the user can have acces due > to group or user > rights. > BUT, how do I combine a printing error message and an exit > within the unless ? > Right now it always prints, and only exits when I have no > permissions. The semicolon makes the exit a separate statement, so you need a comma. However, the comma will be treated as part of the print unless you use parens. So the answer is: print("Invalid login"), exit unless $u{$pwuser} || $g{$group}; > > Thanks for all your previous help!! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]