David Gilden am Samstag, 18. Februar 2006 16.29: > Good morning, Good evening here ;-)
> I would like to populate a hash from a csv text file, then test for the > existence of a value in the hash. If not found return an error message and > exit. > ithe text file will have the format of: > # whitelist created 2/18/06 or some other comment on the first line > name1,nick1 > --- > name25,nick25 > --- I suppose the '---'s are not part of the file? Don't forget: use strict; use warnings; > my $approvedUser = pram('approvedUser'); # cgi.pm > > my $whitelist = './'whitelist.txt; This line leeds to a syntax error, look at/after the second single quote. > my %hash my %hash; > open(EL,"$whitelist") || &errorMessage( "Read in, Could not find > $whitelist, $!"); No need to double quote $whitelist in the first line. You use an error sub to write an error out and to exit with an *ok* exit status. although the script won't do what it shold. The better way would be to die: open (EL, '<', $whitelist) or die "Read in, Could not find $whitelist, $!"; > my @lines = <EL>; > close(EL); close EL or die $!; > foreach(@lines){ > next if /#/; This skips also lines with a '#' not at the beginning. next if /\s*#/; > my($key,$val)= split (@lines,","); You are within a loop processing a single line of @lines (which is hold in $_), and did not read perldoc -f split: my($key,$val)= split ',', $_; > $hash{$key} = $val; > } > close(EL); EL is alredy closed. > if (!defined $approvedUser =~ %hash){ > &errorMessage( "your not an authorized user") > } print "your not an authorized user" unless exists $hash{$approvedUser}; This is a simple test for existence of a key in a hash. See perldoc -f exists > errorMessage(message){ > shift; > print "$_\n"; > exit; > } No need for that anymore. > How close am I to having a working script, I have guessed at some of syntax > here, and am not familiar with using 'defined'. use strict will reveal syntax guesses. BTW, there is also a short way to get the data into %hash. Before just using it, please read the man pages for map and grep. (The usage of __DATA__ and <DATA> is a handy way to simulate file input). === #!/usr/bin/perl use strict; use warnings; my %hash=map {split ',', $_} grep {$_!~/\s*#/} (<DATA>); use Data::Dumper; print Dumper \%hash; __DATA__ # a comment name1,nick1 name25,nick25 === hth, Hans -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>