Created
November 28, 2011 05:30
-
-
Save numberwhun/1399223 to your computer and use it in GitHub Desktop.
Process an inline config file into a hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################################### | |
# This code takes a config file that contains a single line of data that has all of | |
# the needed values separated by a pipe (|) character and parses each line | |
# into an array. Then, each element of the array is assigned to its respective key | |
# in the hash. | |
# | |
# Here is an example of what the config file should look like: | |
# | |
# /export/home/partd001/|subconnector|rck6701ftps02.bcbsmamd.net|sbsbprod|ty5jk3h | |
# | |
##################################################################################### | |
use strict; | |
use warnings; | |
# Open the config file into a file handle | |
open(CFGFILE, "<test2.cfg"); | |
# Initialize the variables you will be using in the code | |
my $key; | |
my $value; | |
my @cfgvalues; | |
my %config; | |
# Read line from the config file into an array, splitting the line on the | |
# pipe character. | |
while(<CFGFILE>) | |
{ | |
chomp($_); | |
@cfgvalues = split(/\|/, $_); | |
} | |
# Assign key/value pairs to the hash using the elements of the array created | |
# above. | |
$config{'ROOTDIR'} = $cfgvalues[0]; | |
$config{'CLIENT'} = $cfgvalues[1]; | |
$config{'TWSERVER'} = $cfgvalues[2]; | |
$config{'TWUSER'} = $cfgvalues[3]; | |
$config{'TWPWD'} = $cfgvalues[4]; | |
# This just prints out the entire has in key/value paired lines | |
for $key ( keys %config) { | |
$value = $config{$key}; | |
print("$key => $value \n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment