David Gilden wrote:
> Good morning,

Hello,

> I would like to populate a hash from a csv text file,

Do you really think that you need to?

> then test for the existence  of a value in the hash.

A value?  Or a key?

perldoc -f exists

> 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
> ---
> 
> my $approvedUser = pram('approvedUser'); # cgi.pm
> 
> my $whitelist = './'whitelist.txt;

That is a syntax error, perhaps you meant:

my $whitelist = './whitelist.txt';

> my %hash
> 
> open(EL,"$whitelist") ||  &errorMessage( "Read in, Could not find $whitelist, 
> $!");

perldoc -q quoting

You are using an ampersand on the subroutine which has certain side-effects
that may not be desirable.

perldoc perlsub

> my @lines = <EL>;

You are reading the whole file into memory but that is not really nessesary.

> close(EL);
> 
> foreach(@lines){
>     next if /#/;
>     my($key,$val)= split (@lines,",");

perldoc -f split
       split /PATTERN/,EXPR,LIMIT

split() uses the first argument as a regular expression pattern so assuming
@lines has 23 elements in it, it will be converted to the number 23 and that
will be used as the pattern to split on:

    my($key,$val)= split (/23/,",");

Since the string "," has no '23' (or any digits at all) in it, it will be
returned unaltered and assigned to the $key variable.

>    $hash{$key} = $val;
>  }
> close(EL);
> 
> if (!defined $approvedUser =~ %hash){

The expression to the right of the binding operator (=~) will be treated as a
regular expression pattern and a hash in scalar context evaluates to a number
so your expression is something like:

if ( !defined $approvedUser =~ m-5/8- ){

The match operator will never return an undefined value so your error message
will never print.

>     &errorMessage( "your not an authorized user")
> }
> 
> 
> errorMessage(message){

perldoc perlsub

If you want to define a subroutine you have to use:

sub errorMessage {

> shift;

shift() removes the first element of an array and returns it.  Inside a
subroutine, if you don't specify an array it defaults to the @_ array which
contains all the values passed to the subroutine.  You are not assigning the
return value of shift() to a variable so it disappears.

> print "$_\n";

You have not assigned a value to $_ so nothing (except a newline) will be 
printed.

> exit;

You should usually exit() with a value that signifies success or failure.
Most OSs use zero for success and a non-zero value for failure.

> }
> 
> 
> How close am I to having a working script, I have guessed at some of syntax 
> here, and am not familiar 
> with using 'defined'.

If you just want to see if a certain name is present in the file you can do
something like this:

use warnings;
use strict;

sub errorMessage {
    my $message = shift;
    print "$message\n";
    exit 1;
    }

my $approvedUser = param( 'approvedUser' ); # cgi.pm
my $whitelist = 'whitelist.txt';
open EL, '<', $whitelist
    or errorMessage( "Read in, Could not open $whitelist, $!");

my ( $key, $val );
while ( <EL> ) {
    next if /#/;
    ( $key, $val ) = /^([^,]+),(.+)/;
    last if $approvedUser eq $key;
    }

errorMessage( 'you are not an authorized user' )
    unless defined $key;

__END__


If you do really need a hash then:

use warnings;
use strict;

sub errorMessage {
    my $message = shift;
    print "$message\n";
    exit 1;
    }

my $approvedUser = param( 'approvedUser' ); # cgi.pm
my $whitelist = 'whitelist.txt';
open EL, '<', $whitelist
    or errorMessage( "Read in, Could not open $whitelist, $!");

my %hash;
while ( <EL> ) {
    next if /#/;
    my ( $key, $val ) = /^([^,]+),(.+)/;
    $hash{ $key } = $val;
    }

errorMessage( 'you are not an authorized user' )
    unless exists $hash{ $approvedUser };

__END__




John
-- 
use Perl;
program
fulfillment

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


Reply via email to