HI kabana,
This is something I do a lot so I wrote a little "utils" package to contain
this and other common actions.
Here is the subroutine.
Pass it the name of the config file, a hash reference and an optinal
delimeter. You will get back a hash populated with the details of the
config file
Hope this helps,
Joe
sub datafileToHash {
my ($filename, $hash, $delimeter);
$filename = shift;
$hash = shift;
$delimeter = shift;
if (not defined $filename || not defined $hash) { return 0; }
if (not defined $delimeter) { $delimeter = "="; }
open (DATAFILE, $filename) or return 0;
while (<DATAFILE>) {
chomp;
s/^(\s+)//;
unless ($_ =~ m"^\#" || length($_) == 0) {
@hashvalues = split($delimeter, $_, 2);
$hashvalues[0] =~ s/(\s+)$//;
$hashvalues[1] =~ s/^(\s+)//;
$hash->{$hashvalues[0]} = $hashvalues[1];
}
}
return 1;
}
[EMAIL PROTECTED]@listserv.ActiveState.com on 09/19/2002 12:21:38
Sent by: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
cc:
Subject: [Perl-unix-users] portions of strings
Hi,
1. I need to read a configuration file of name/value pairs and be able to
retrieve 'values of names'
An eg. format of configuration file is
DIR=d:\test
ACTION=BUY
ERROR=2345
ACTION=SELL
I want to write a subroutine to retrive the value d:\test with an input
string 'DIR' say
eg. get_value('DIR') should return d:\test
eg. get_value('ERROR') should return 2345
2. It sould also check for duplicate 'names' (ie more than one)
eg. get_value('ACTION') should return an error cos the name ACTION appears
more than once in the config file.
Any help would be greatly appreciated.
Kwabena
_____________________________________________________
This transmission has been issued by a member of the HSBC Group
"HSBC" for the information of the addressee only and should not be
reproduced and / or distributed to any other person. Each page attached
hereto must be read in conjunction with any disclaimer which forms part
of it. Unless otherwise stated, this transmission is neither an offer nor
the
solicitation of an offer to sell or purchase any investment. Its contents
are
based on information obtained from sources believed to be reliable but
HSBC makes no representation and accepts no responsibility or liability as
to its completeness or accuracy.
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs