| |||||||||||
| |||||||||||
Read INI Posted by yamaha102 (yamaha102), 10 February 2008 How to read a parameter from INI file.let say when i call the Header1 and A, it will give me 1. I tried &ReadINI $header{'A'}; but it return "Use of uninitialized value in print..." Thanks. ---test.ini---- [Header1] A=1 B=2 [Header2] A=3 B=2 ----code ---- use strict; use warnings; my %section=(); my %global=(); my $iniFile = "c:/perl/test.ini" ; &ReadINI ; sub ReadINI { open (INI, $iniFile) || die "could not open $iniFile - $!" ; while (my $line = <INI>) { if (($line =~ /^\;/)||($line =~ /^\s+$/)) { #ignore these lines coz they are blank } else { if ($line =~ /^\[(.+)\]/) { my $section = $1 ; push (my @sections,$section) ; } else { chomp $line ; (my $key,my $value) = split (/=/, $line) ; $section{$key} = $value ; } } } Posted by KevinAD (KevinAD), 10 February 2008 Hard to say because there are no lines in the code you posted that are printing anything. But this looks wrong:if ($line =~ /^\[(.+)\]/) { my $section = $1 ; push (my @sections,$section) ; } do you see the problem? Posted by yamaha102 (yamaha102), 10 February 2008 Thanks for your time.I use this statement to print the output. print &ReadINI $header{'A'}; Regarding push (my @sections,$section) ; statement.. I do not see the problem... if you see the problem , try to teach me. Thanks. As right after the statement I add in the print push (my @sections,$section); I could get the [header1] and [Header2]... Posted by KevinAD (KevinAD), 10 February 2008 When you declare a variable with "my", any value it may have had before is gone, and it is only visible to the block the variable is scoped to. So the array @sections is not accessible outside the "if" block you have declared it in:if ($line =~ /^\[(.+)\]/) { my $section = $1 ; push (my @sections,$section) ; } any value @sections had before is lost each time the "if" block is evaluated because you redeclare @sections with "my" each time the block is evaluated. Declare @sections at the top of the script like you do the hashes. This is also not necessary: (my $key,my $value) = split (/=/, $line) ; better written as: my ($key, $value) = split (/=/, $line) ; Posted by yamaha102 (yamaha102), 11 February 2008 Noted. Thanks.Back to the question, how I do I to get a parameter from the INI file? Posted by Custard (Custard), 11 February 2008 Hiya, I know you like a challenge so I'm not going to take that away from you, but have you had a look on CPAN? http://search.cpan.org/~kirsle/Config-INI-Simple-0.02/lib/Config/INI/Simple.pm Looks like it will read your INI into a hash of hashes and also allow writing it back to the file. hth B Posted by KevinAD (KevinAD), 11 February 2008 on 02/11/08 at 19:07:52, Custard wrote:
I'm not saying there is anything wrong with that suggestion, especially since the module is written by a forum buddy of mine, but it is only version .01 and is a very new module that has not been tested and has 3 bug reports listed (which I have not read what they are). Having said that, it would be cool if yamaha102 gave it a try to see how well it works. There is another module Config::Tiny, http://search.cpan.org/~adamk/Config-Tiny-2.12/ is a well tested module that reads/writes config ini files and is sort of a defacto module to use for that purpose. Posted by yamaha102 (yamaha102), 12 February 2008 Hi all,Thanks for the advices. Posted by yamaha102 (yamaha102), 12 February 2008 Hi all,Thanks for the advices. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |