Hi guys !

A little problem. I have a parser (not mine) for a configuration file (smb.conf if someone knows). this file as from the format:
[name1] key1 = value1 key2 = value2 ... [name2] key1 = value1 key2 = value2 ... # and so on
The problem is when I have something like this:
[name1] key1 = value1 key2 = value2 ... [name2] [name3] key1 = value1 key2 = value2 ...
as you can see name2 has no values, this thing is ok (default values are taken), but the parser I have doesn't return it, here is it's code:
sub smbconf_parse { my $smbconf = shift; my %smbconf; my $share = ''; if (! open(SMB, $smbconf)) { warn "Couldn't read smbconf file $smbconf\n"; warn "$!\n"; return 0; } while (<SMB>) { s/^\s+//g; s/\s+$//g; next if (/^$/); next if (/^\#/); next if (/^\;/); if (/^\[(.*)\]/) { $share = $1; ####### } else { my ($key, $value) = (/^(.*) ?\= (\S.*)/); $key =~ s/\s+$//; if ($value =~ /^\"(.*)\"$/) { $smbconf{$share}{$key} = $1; } elsif ($value =~ /\,/) { my @value = split(/\,/, $value); $smbconf{$share}{$key} = [ @value ]; } else { $smbconf{$share}{$key} = $value; } } } close SMB; return \%smbconf; }
I can see the problem, no hash entry is opened if it didn't find any key=value pairs, so I tried adding something like
$smbconf{$share} = ''; # just to open the entry
right after the line with $share = $1;, but that seemed to mess the whole parsing.

anyone has an idea ?

Hotshot

In reply to hash reference by hotshot

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.