For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > populating a Hash









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author populating a Hash
David Gilden

2006-02-18, 6:56 pm

Good morning,

I would like to populate a hash from a csv text file, then test for the exi=
stence 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
---

my $approvedUser =3D pram('approvedUser'); # cgi.pm

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

my %hash

open(EL,"$whitelist") || &errorMessage( "Read in, Could not find $whitelis=
t, $!");
my @lines =3D <EL>;
close(EL);

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

if (!defined $approvedUser =3D~ %hash){
&errorMessage( "your not an authorized user")
}


errorMessage(message){
shift;
print "$_\n";
exit;
}


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

Thanks for any guidance.

Regards from Cow Town,
David Gilden
(kora musician / audiophile / webmaster @ www.coraconnection.com / Ft. Wor=
th, TX, USA)=20
Hans Meier

2006-02-18, 6:56 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com