Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reading .ini file without using a module

Reply
Thread Tools

reading .ini file without using a module

 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      03-16-2011
hello group,

i understand that there are modules to parse and read .ini files but
just to understand data structures better i was playing with this.
the following code is from perl developers dictionary:

use strict;
use warnings;
use Data:umper;

open my $fh, '<', "my.ini" or die "$!\n";
my $ini = {};

{
local $/ = "";
while (<$fh>) {
next unless ( s/\[([^]]+)\]// );
my $header = $1;
$ini->{$header} = {};
while (m/(\w+)=(.*)/g) {
$ini->{$header}->{$1} = $2;
}
}
}
close $fh;

print Dumper $ini;


__DATA__
[Build]
BuildID=20110209115208
Milestone=2.0b11
SourceStamp=f9d66f4d17bf
SourceRepository=http://hg.mozilla.org/mozilla-central
; This file is in the UTF-8 encoding

[Strings]
Title=SeaMonkey Update
Info=SeaMonkey is installing your updates and will start in a few
moments


i could not figure a way to do something like

print $ini->{Build}->{Milestone}
# 2.0b11

print $ini->{Strings}->{Title}
# SeaMonkey Update

could somebody lend me their hand to figure how to make this work?

thanks
 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      03-16-2011
Στις 16/3/2011 10:03 μμ, ο/η alfonsobaldaserra *γραψε:
> use strict;
> use warnings;
> use Data:umper;
>
> open my $fh, '<', "my.ini" or die "$!\n";
> my $ini = {};
>
> {
> local $/ = "";
> while (<$fh>) {
> next unless ( s/\[([^]]+)\]// );
> my $header = $1;
> $ini->{$header} = {};
> while (m/(\w+)=(.*)/g) {
> $ini->{$header}->{$1} = $2;
> }
> }
> }
> close $fh;
>
> print Dumper $ini;
>
>
> __DATA__
> [Build]
> BuildID=20110209115208
> Milestone=2.0b11
> SourceStamp=f9d66f4d17bf
> SourceRepository=http://hg.mozilla.org/mozilla-central
> ; This file is in the UTF-8 encoding
>
> [Strings]
> Title=SeaMonkey Update
> Info=SeaMonkey is installing your updates and will start in a few
> moments
>
>
> i could not figure a way to do something like
>
> print $ini->{Build}->{Milestone}
> # 2.0b11
>
> print $ini->{Strings}->{Title}
> # SeaMonkey Update
>
> could somebody lend me their hand to figure how to make this work?
>
> thanks


try to create the my.ini with the same data and give a shot !
 
Reply With Quote
 
 
 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      03-17-2011
> try to create the my.ini with the same data and give a shot !

it was a very serious mistake. i am very sorry and thank you very
much for correcting me.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading LAST line from text file without iterating through the file? Robin Wenger Java 191 03-26-2011 06:19 PM
Python 2.6 SSL module: Fails on key file error, with Errno 336265225,without a key file. John Nagle Python 2 04-19-2010 04:35 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muoz ASP .Net 3 06-04-2005 10:56 AM
How to allocate mem without using malloc() & free without using free() Rajshekhar C Programming 5 03-29-2005 06:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57