Read a configuration file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 2,174:
favouritefruit = banana
needspeeling = 1
seedsremoved = 0 </lang> =={{header|J}}==
 
=={{header|J}}==
 
<lang j>require'regex'
Line 2,209 ⟶ 2,207:
needspeeling = 1
seedsremoved = 0 </lang>
 
=={{header|JavaScript}}==
In JavaScript using an object makes more sense than local variables. This function takes our config file in plain text as the parameter.
 
<lang javascript>function parseConfig(config) {
// this expression matches a line starting with an all capital word,
// and anything after it
var regex = /^([A-Z]+)(.*)$/mg;
var configObject = {};
// loop until regex.exec returns null
var match;
while (match = regex.exec(config)) {
// values will typically be an array with one element
// unless we want an array
// match[0] is the whole match, match[1] is the first group (all caps word),
// and match[2] is the second (everything through the end of line)
var key = match[1], values = match[2].split(",");
if (values.length === 1) {
configObject[key] = values[0];
}
else {
configObject[key] = values.map(function(value){
return value.trim();
});
}
}
return configObject;
} </lang>
 
The result is an object, which can be represented with this JSON.
 
<lang javascript>{
"FULLNAME": " Foo Barber",
"FAVOURITEFRUIT": " banana",
"NEEDSPEELING": "",
"OTHERFAMILY": [
"Rhu Barber",
"Harry Barber"
]
}
</lang>
 
=={{header|Java}}==
Line 2,328 ⟶ 2,283:
{{out}}
<pre>{otherfamily=[Rhu Barber, Harry Barber], favouritefruit=banana, seedsremoved=false, needspeeling=true, fullname=Foo Barber}</pre>
 
=={{header|JavaScript}}==
In JavaScript using an object makes more sense than local variables. This function takes our config file in plain text as the parameter.
 
<lang javascript>function parseConfig(config) {
// this expression matches a line starting with an all capital word,
// and anything after it
var regex = /^([A-Z]+)(.*)$/mg;
var configObject = {};
// loop until regex.exec returns null
var match;
while (match = regex.exec(config)) {
// values will typically be an array with one element
// unless we want an array
// match[0] is the whole match, match[1] is the first group (all caps word),
// and match[2] is the second (everything through the end of line)
var key = match[1], values = match[2].split(",");
if (values.length === 1) {
configObject[key] = values[0];
}
else {
configObject[key] = values.map(function(value){
return value.trim();
});
}
}
return configObject;
} </lang>
 
The result is an object, which can be represented with this JSON.
 
<lang javascript>{
"FULLNAME": " Foo Barber",
"FAVOURITEFRUIT": " banana",
"NEEDSPEELING": "",
"OTHERFAMILY": [
"Rhu Barber",
"Harry Barber"
]
}
</lang>
 
=={{header|jq}}==
Line 2,752 ⟶ 2,750:
}
</pre>
 
=={{header|Nanoquery}}==
<lang nanoquery>import Nanoquery.IO
Line 3,287 ⟶ 3,285:
}
</lang>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.03}}
 
 
This demonstrates several interesting features of Perl 6, including full grammar support, derived grammars, alternation split across derivations, and longest-token matching that works across derivations. It also shows off Perl 6's greatly cleaned up regex syntax.
<lang perl6>my $fullname;
my $favouritefruit;
my $needspeeling = False;
my $seedsremoved = False;
my @otherfamily;
 
grammar ConfFile {
token TOP {
:my $*linenum = 0;
^ <fullline>* [$ || (\N*) { die "Parse failed at $0" } ]
}
 
token fullline {
<?before .>
{ ++$*linenum }
<line>
[ \n || { die "Parse failed at line $*linenum" } ]
}
 
proto token line() {*}
 
token line:misc { {} (\S+) { die "Unrecognized word: $0" } }
 
token line:sym<comment> { ^^ [ ';' | '#' ] \N* }
token line:sym<blank> { ^^ \h* $$ }
 
token line:sym<fullname> {:i fullname» <rest> { $fullname = $<rest>.trim } }
token line:sym<favouritefruit> {:i favouritefruit» <rest> { $favouritefruit = $<rest>.trim } }
token line:sym<needspeeling> {:i needspeeling» <yes> { $needspeeling = defined $<yes> } }
token rest { \h* '='? (\N*) }
token yes { :i \h* '='? \h*
[
|| ([yes|true|1])
|| [no|false|0]
|| (<?>)
] \h*
}
}
 
grammar MyConfFile is ConfFile {
token line:sym<otherfamily> {:i otherfamily» <rest> { @otherfamily = $<rest>.split(',')».trim } }
}
 
MyConfFile.parsefile('file.cfg');
 
say "fullname: $fullname";
say "favouritefruit: $favouritefruit";
say "needspeeling: $needspeeling";
say "seedsremoved: $seedsremoved";
print "otherfamily: "; say @otherfamily.perl;</lang>
{{out}}
<pre>fullname: Foo Barber
favouritefruit: banana
needspeeling: True
seedsremoved: False
otherfamily: ["Rhu Barber", "Harry Barber"]
</pre>
 
=={{header|Phix}}==
Line 3,991 ⟶ 3,926:
seedsremoved = #f
otherfamily = ("Rhu Barber" "Harry Barber")
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
 
 
This demonstrates several interesting features of Perl 6, including full grammar support, derived grammars, alternation split across derivations, and longest-token matching that works across derivations. It also shows off Perl 6's greatly cleaned up regex syntax.
<lang perl6>my $fullname;
my $favouritefruit;
my $needspeeling = False;
my $seedsremoved = False;
my @otherfamily;
 
grammar ConfFile {
token TOP {
:my $*linenum = 0;
^ <fullline>* [$ || (\N*) { die "Parse failed at $0" } ]
}
 
token fullline {
<?before .>
{ ++$*linenum }
<line>
[ \n || { die "Parse failed at line $*linenum" } ]
}
 
proto token line() {*}
 
token line:misc { {} (\S+) { die "Unrecognized word: $0" } }
 
token line:sym<comment> { ^^ [ ';' | '#' ] \N* }
token line:sym<blank> { ^^ \h* $$ }
 
token line:sym<fullname> {:i fullname» <rest> { $fullname = $<rest>.trim } }
token line:sym<favouritefruit> {:i favouritefruit» <rest> { $favouritefruit = $<rest>.trim } }
token line:sym<needspeeling> {:i needspeeling» <yes> { $needspeeling = defined $<yes> } }
token rest { \h* '='? (\N*) }
token yes { :i \h* '='? \h*
[
|| ([yes|true|1])
|| [no|false|0]
|| (<?>)
] \h*
}
}
 
grammar MyConfFile is ConfFile {
token line:sym<otherfamily> {:i otherfamily» <rest> { @otherfamily = $<rest>.split(',')».trim } }
}
 
MyConfFile.parsefile('file.cfg');
 
say "fullname: $fullname";
say "favouritefruit: $favouritefruit";
say "needspeeling: $needspeeling";
say "seedsremoved: $seedsremoved";
print "otherfamily: "; say @otherfamily.perl;</lang>
{{out}}
<pre>fullname: Foo Barber
favouritefruit: banana
needspeeling: True
seedsremoved: False
otherfamily: ["Rhu Barber", "Harry Barber"]
</pre>
 
Line 4,167 ⟶ 4,166:
otherfamily(2) = Harry Barber
</pre>
 
 
=={{header|Run BASIC}}==
10,333

edits