On Fri, May 29, 2020 at 05:02:15PM -0700, ToddAndMargo via perl6-users wrote: > Hi All, > > I an not figure out how to read the hash. > > > ~~~~~~~~~~~ ini.test.pl6.ini ~~~~~~~~~~~~~ > # Raku: Confug::INI test INI > # edit at your own risk > > [Backup paramters] > target=B:\myDocsBackp\backup1 > partition=BACKUP > > [eMail] > smtp=smtp.bozo.com > address=b...@theclown.com > port=587 > ~~~~~~~~~~~ /ini.test.pl6.ini ~~~~~~~~~~~~~ > > > ~~~~~~~~~~~ ini.test.pl6 ~~~~~~~~~~~~~ > #! /usr/bin/env raku > > #`{ > # zef install Config > # zef install Config::INI > > > > https://github.com/tadzik/perl6-Config-INI/blob/master/lib/Config/INI.pm > line 45 starts documtation > > use Config::INI; > my %hash = Config::INI::parse_file('config.ini'); > #or > %hash = Config::INI::parse($file_contents); > say %hash<_><root_property_key>; > say %hash<section><in_section_key>; > > This module provides 2 functions: parse() and parse_file(), both taking > one C<Str> argument, where parse_file is just parse(slurp $file). > Both return a hash which keys are either toplevel keys or a section > names. For example, the following config file: > > foo=bar > [section] > another=thing > > would result in the following hash: > } > # { '_' => { foo => "bar" }, section => { another => "thing" } } > > > use Config; > use Config::INI; > > my Str $IniFile = slurp "ini.test.pl6.ini"; > my %hash = Config::INI::parse($IniFile); > > dd %hash; > print "\n"; > > for %hash.kv -> $key, $value { print " hash<$key> = [$value]\n\n"; } > print "\n"; > ~~~~~~~~~~~ /ini.test.pl6 ~~~~~~~~~~~~~ > > ~~~~~~~~~~~~~ run the test ~~~~~~~~~~~~ > $ ini.test.pl6 > > Hash %hash = {"Backup paramters" => ${:partition("BACKUP"), > :target("B:\\myDocsBackp\\backup1")}, > :eMail(${:address("bozo\@theclown.com"), :port("587"), > :smtp("smtp.bozo.com")})} > > hash<Backup paramters> = [partition BACKUP > target B:\myDocsBackp\backup1] > > hash<eMail> = [address b...@theclown.com > port 587 > smtp smtp.bozo.com] > ~~~~~~~~~~~~~ /run the test ~~~~~~~~~~~~ > > > How do I read the hash so I get section > `[eMail]` `smtp`? (Or any of the other values > as well.)
If you want that specific value, dd %hash<eMail><smtp> If some of the keys are in variables, try this: my $section = "eMail"; my $key = "smtp"; dd %hash{$section}{$key}; So %hash is a hash of hashes: the keys of %hash are the names of the sections, the values are hashes containing the key/value contents of the sections. The keys of %hash<eMail> are the names of the variables defined in the "eMail" section, the values of %hash<eMail> are the, well, values of the things in the "eMail" section. G'luck, Peter -- Peter Pentchev r...@ringlet.net r...@debian.org p...@storpool.com PGP key: http://people.FreeBSD.org/~roam/roam.key.asc Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
signature.asc
Description: PGP signature