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?

In reply to property file parsing by mifflin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.