Wednesday, September 26, 2012

Reading complete property file and return hash

# Function Name    : readInputFile
# Input                    : 1. Input file name
#                              2. Delimiter.  if not specified then = will be used
# Output                : None
# Description            : This subroutine will parse input file having key-value pair and create hash.  # as first character in file will be ignored
sub readInputFile($$)
{
    my ($file, $delim) = @_;
    $file = &convertSlash(&trim($file));
    $delim = &trim($delim);

    my %list;

    if ($delim eq "")
    {
        $delim = "=";
    }

    open (FI, $file);
    while()
    {
        my $line = $_;
        chomp $line;
        $line = &trim($line);

        unless ((substr($line, 0, 1) eq "#")  || ($line eq ""))
        {
            my ($key, $value) = split($delim, $line);
            $key = &trim($key);
            $value = &trim($value);

            if (exists $list{$key})
            {
                $value =  $list{$key}."::$value";
            }

            $list{$key} = $value;
        }
    }
    close (FI);
    return %list;
}

No comments:

Post a Comment