obonaventure has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me figure out why this code does not work. A little background first. I program exclusively on Win32 (and sometimes Win9x (eek!!)) and I am writing a program that needs to read in INI file and read each entry into an array. This is necessary because the number of entries is unknown and will be determined by the user. Below is an example program that is supposed to read each entry into an array and then print each entry out to the screen. The problem is that it is not working and I have used this EXACT code in another program and it works perfectly.
use Config::IniFiles; $cfg = new Config::IniFiles( -file => uc "test.ini", -nocase => 1); @FileExten = $cfg -> Parameters(FileExten); foreach $FileExten (@FileExten) { $FileExt = uc $cfg -> val(FileExten, $FileExten); print "$FileExtension is $FileExt\n"; }
CONTENTS OF TEST.INI
[FileExten] FileExten1 = Value1 FileExten2 = Value2 FileExten3 = Value3 FileExten4 = Value4 FileExten5 = Value5 FileExten6 = Value6 FileExten7 = Value7 FileExten8 = Value8 FileExten9 = Value9

Replies are listed 'Best First'.
Re: Reading an INI file in an array
by Tanktalus (Canon) on Sep 07, 2005 at 03:49 UTC

    Comments:

    • use strict; use warnings;
    • Clean up all the warnings it gives you.
    • Oh, and you shouldn't uc the filename. For one thing, Windows isn't case sensitive anyway, so it's just wasting CPU cycles. For another, it makes it harder to read for those of us on other platforms (I thought I was supposed to save your data as "test.ini" not "TEST.INI")
    You have a number of barewords there that aren't going to be doing what you think they're doing - strict would have told you about them, and then you could put them in single quotes and all would work fine.

Re: Reading an INI file in an array
by pg (Canon) on Sep 07, 2005 at 04:30 UTC

    Made some minor changes, your code works. See comments for details.

    use Config::IniFiles; use strict; #try to have this all the time use warnings; #try to have this all the time my $cfg = new Config::IniFiles( -file => uc "test.ini", -nocase => 1); + #uc is not needed, but does not cause trouble either my @FileExten = $cfg->Parameters("FileExten"); #change bareword to a s +tring foreach my $FileExten (@FileExten) #explicitly declare your variable, +same with other places { my $FileExt = uc $cfg->val("FileExten", $FileExten); #again, bare +word print "FileExtension is $FileExt\n"; #you don't have a variable c +alled $FileExtension, so I removed the $ }