I have a properties file that is structured like the typical string=value where the string portion is formated like key1.key2...keyn
the problem is that I don't know the 'n' (how many key portions are in the string) and I'm looking for a way to be able to dynamincally handle it.
Here is a small sample of the properties file..
language_id.en_US=-1
language_id.es_US=-11
language_id.en_CA=-12
language_id.fr_CA=-13
langid.SPMEX=es_US
langid.FRCAN=fr_CA
altlang.US=SPMEX
altlang.CA=FRCAN
itemattr.newpart.seq=1
itemattr.newpart.name=New Part
itemattr.newpart.image1.en_US=newitem_icon.gif
itemattr.newpart.image1.es_US=newitem_icon_sp.gif
itemattr.newpart.image1.en_CA=newitem_icon.gif
itemattr.newpart.image1.fr_CA=newitem_icon_fr.gif
itemattr.watersaver.seq=2
itemattr.watersaver.name=New Part
itemattr.watersaver.image1.en_US=newitem_icon.gif
itemattr.watersaver.image1.es_US=newitem_icon_sp.gif
itemattr.watersaver.image1.en_CA=newitem_icon.gif
itemattr.watersaver.image1.fr_CA=newitem_icon_fr.gif
Here is the relevant section of my code that parses this file...
my %properties;
my $propfilename = $opt{p} ? $opt{p} : "$FindBin::Bin/${FindBin::Scrip
+t}.properties";
print "reading properties from $propfilename\n" if $opt{v};
my $pfh = IO::File->new("<$propfilename") or die "unable to open prope
+rties file $propfilename, $!";
while (my $propline = <$pfh>) {
next if $propline =~ /^#/;
print $propline if $opt{V};
chomp $propline;
my ($key, $value) = split(/=/, $propline);
my ($key1, $key2, $key3, $key4) = split(/\./, $key);
if (defined $key4) {
$properties{$key1}{$key2}{$key3}{$key4} = $value;
}
elsif (defined $key3) {
$properties{$key1}{$key2}{$key3} = $value;
}
elsif (defined $key2) {
$properties{$key1}{$key2} = $value;
}
elsif (defined $key1) {
$properties{$key1} = $value;
}
}
$pfh->close;
What I don't like is all the hard coding of handling n keys. As you can see I only hand up to four. Is there a better way to do this?