Update a configuration file: Difference between revisions

Content added Content deleted
Line 2,754: Line 2,754:
NUMBEROFSTRAWBERRIES 62000
NUMBEROFSTRAWBERRIES 62000
</pre>
</pre>

=={{header|PHP}}==

<lang PHP><?php

$conf = file_get_contents('update-conf-file.txt');

// Disable the needspeeling option (using a semicolon prefix)
$conf = preg_replace('/^(needspeeling)(|\s*\S*)$/mi', '; $1', $conf);

// Enable the seedsremoved option by removing the semicolon and any leading whitespace
$conf = preg_replace('/^;?\s*(seedsremoved)/mi', '$1', $conf);

// Change the numberofbananas parameter to 1024
$conf = preg_replace('/^(numberofbananas)(|\s*\S*)$/mi', '$1 1024', $conf);

// Enable (or create if it does not exist in the file) a parameter for numberofstrawberries with a value of 62000
if (preg_match('/^;?\s*(numberofstrawberries)/mi', $conf, $matches)) {
$conf = preg_replace('/^(numberofstrawberries)(|\s*\S*)$/mi', '$1 62000', $conf);
} else {
$conf .= 'NUMBEROFSTRAWBERRIES 62000' . PHP_EOL;
}

echo $conf;</lang>

{{in}}
<pre># This is a configuration file in standard configuration file format
#
# Lines begininning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.

# The first word on each non comment line is the configuration option.
# Remaining words or numbers on the line are configuration parameter
# data fields.

# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.

# This is a favourite fruit
FAVOURITEFRUIT banana

# This is a boolean that should be set
NEEDSPEELING

# This boolean is commented out
; SEEDSREMOVED

# How many bananas we have
NUMBEROFBANANAS 48</pre>

{{out}}
<pre># This is a configuration file in standard configuration file format
#
# Lines begininning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.

# The first word on each non comment line is the configuration option.
# Remaining words or numbers on the line are configuration parameter
# data fields.

# Note that configuration option names are not case sensitive. However,
# configuration parameter data is case sensitive and the lettercase must
# be preserved.

# This is a favourite fruit
FAVOURITEFRUIT banana

# This is a boolean that should be set
; NEEDSPEELING

# This boolean is commented out
SEEDSREMOVED

# How many bananas we have
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==