Kwabena,

Your problem has 2 parts - the first is to load your data file and check for
duplicates, and the second is to be able to look for items of interest and
retrieve the values.

Here's a light-duty example of how to do this using hashes.

hth,

Craig Arnold

=========================================================================
Source:
#!/usr/local/bin/perl -w

use FileHandle ;
my $fh = new FileHandle ;
my $fni = shift or die "I need an input filename - try again...\n" ;
my %log = () ;

open($fh, $fni) or die "Unable to open input file ($!)" ;

# first load the hash from the data file
while(<$fh>)
{
        chomp ;
        s/#.*// ;              # strip comments (anything beginning with #)
        next if(! /=/) ;
        my @temp = split /=/ ; # assumes "=" can't be part of any key or
value
        if($log{$temp[0]})
        {
                print "Duplicate entry: $temp[0] (First line:
$log{$temp[0]}{line}  Current line: $.)\n" ;
                next ;
        }
        
        @temp = grep { s/\s+//g ; $_ } @temp ; # strip all whitespaces - you
may not want this
        $log{$temp[0]}{line} = $. ;            # if you care about the line
numbers.
        $log{$temp[0]}{value} = $temp[1] ;
}

#------------------------------------
# put whatever code you want here...
print "\n" ;
print map "$_: ".get_val($_)."\n", qw(DIR test Dir ERROR ACTION) ;
#------------------------------------

# subroutine to return a string for undefined entries
sub get_val
{
        return $log{shift()}{value} || "undefined" ;
}
=========================================================================
Data file:
# comment line
DIR=d:\test
ACTION=BUY
ERROR=2345
ACTION=SELL
=========================================================================
Output:
D:\Made\perl>kwabena.pl kwabena.dat
Duplicate entry: ACTION (First line: 3  Current line: 5)

DIR: d:\test
test: undefined
Dir: undefined
ERROR: 2345
ACTION: BUY
=========================================================================

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 19, 2002 4:22 AM
To: [EMAIL PROTECTED]
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

Reply via email to