Jump to content

Update a configuration file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 2,053:
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000</pre>
 
 
=={{header|Julia}}==
Line 2,615 ⟶ 2,614:
# How many bananas we have
NUMBEROFBANANAS 48</lang>
 
=={{header|Perl 6}}==
Implemented as a command-line script which can make arbitrary in-place updates to such config files.
 
Assuming that the script is saved as <tt>conf-update</tt> and the config file as <tt>test.cfg</tt>, the four changes required by the task description could be performed with the command:
 
<pre>conf-update --/needspeeling --seedsremoved --numberofbananas=1024 --numberofstrawberries=62000 test.cfg</pre>
 
The script:
 
<lang perl6>use File::Temp;
 
my ($tmpfile, $out) = tempfile;
 
sub MAIN ($file, *%changes) {
%changes.=map({; .key.uc => .value });
my %seen;
 
for $file.IO.lines {
when /:s ^ ('#' .* | '') $/ {
say $out: ~$0;
}
when /:s ^ (';'+)? [(\w+) (\w+)?]? $/ {
next if !$1 or %seen{$1.uc}++;
my $new = %changes{$1.uc}:delete;
say $out: format-line $1, |( !defined($new) ?? ($2, !$0) !!
$new ~~ Bool ?? ($2, $new) !! ($new, True) );
}
default {
note "Malformed line: $_\nAborting.";
exit 1;
}
}
 
say $out: format-line .key, |(.value ~~ Bool ?? (Nil, .value) !! (.value, True))
for %changes;
 
$out.close;
 
copy $tmpfile, $file;
 
sub format-line ($key, $value, $enabled) {
("; " if !$enabled) ~ $key.uc ~ (" $value" if defined $value);
}</lang>
 
=={{header|Phix}}==
Line 3,311 ⟶ 3,265:
(set! numberofstrawberries 62000)
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
Implemented as a command-line script which can make arbitrary in-place updates to such config files.
 
Assuming that the script is saved as <tt>conf-update</tt> and the config file as <tt>test.cfg</tt>, the four changes required by the task description could be performed with the command:
 
<pre>conf-update --/needspeeling --seedsremoved --numberofbananas=1024 --numberofstrawberries=62000 test.cfg</pre>
 
The script:
 
<lang perl6>use File::Temp;
 
my ($tmpfile, $out) = tempfile;
 
sub MAIN ($file, *%changes) {
%changes.=map({; .key.uc => .value });
my %seen;
 
for $file.IO.lines {
when /:s ^ ('#' .* | '') $/ {
say $out: ~$0;
}
when /:s ^ (';'+)? [(\w+) (\w+)?]? $/ {
next if !$1 or %seen{$1.uc}++;
my $new = %changes{$1.uc}:delete;
say $out: format-line $1, |( !defined($new) ?? ($2, !$0) !!
$new ~~ Bool ?? ($2, $new) !! ($new, True) );
}
default {
note "Malformed line: $_\nAborting.";
exit 1;
}
}
 
say $out: format-line .key, |(.value ~~ Bool ?? (Nil, .value) !! (.value, True))
for %changes;
 
$out.close;
 
copy $tmpfile, $file;
 
sub format-line ($key, $value, $enabled) {
("; " if !$enabled) ~ $key.uc ~ (" $value" if defined $value);
}</lang>
 
=={{header|REXX}}==
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.