Given the following file:
[blah]
stuff1=farqasaur
stuff2=loud sucking sound
lets load it into a perl hash where we can get values by using the key as an index.
my $textfile = "properties.properties";
my %properties;
open TEXTFILE, $textfile or die("Could not open text file '" . $textfile . "': $!");
foreach $line (<TEXTFILE>) {
chomp($line); # remove the newline from $line.
@tokens = split(/=/, $line);
# do line-by-line processing.
printf(@tokens . "\n");
if(@tokens == 2 ) { # if this is a key value pair, save it to the hash
$properties{@tokens[0]}=@tokens[1];
printf("\n Saving " . @tokens[0]);
}
}
Now you can access the hash like so:
foreach $kv (%properties) {
printf($kv[0] . "\n");
}
printf("\nstuff1:" . $properties{'stuff1'});
No comments:
Post a Comment