G'day hellohello1,
Welcome to the monastery.
Here's a short script that tests solutions offered:
#!/usr/bin/env perl
use strict;
use warnings;
my @tests = ('0', '1', '000', '101', '010', '1 * 0 = 0');
my $format = '%9s | ' x 4 . "\n";
printf $format => qw{initial boundary start/end assign};
for (@tests) {
my ($init, $bound, $start, $assign) = ($_) x 4;
$bound =~ s/\b0\b/1/;
$start =~ s/^0$/1/;
$assign = 1 if $assign == 0;
printf $format => $init, $bound, $start, $assign;
}
Output:
initial | boundary | start/end | assign |
0 | 1 | 1 | 1 |
1 | 1 | 1 | 1 |
000 | 000 | 000 | 1 |
101 | 101 | 101 | 101 |
010 | 010 | 010 | 010 |
Argument "1 * 0 = 0" isn't numeric in numeric eq (==) at ./pm_example.
+pl line 16.
1 * 0 = 0 | 1 * 1 = 0 | 1 * 0 = 0 | 1 * 0 = 0 |
Results:
-
Word boundaries fail with input like '1 * 0 = 0'
-
Anchoring to the start and end of the string is successful in all instances
-
Assignment conditional on equality to zero fails in two ways:
-
A string that is not '0' but is evaluated as being numerically equal to zero (i.e. '000')
-
A string that cannot be evaluated as being numeric generates a warning (i.e. '1 * 0 = 0')
So, with the data and solutions used here, only s/^0$/1/ works consistently.
You can change @tests to a list of representative examples from your data to get more realistic output.
You can add tests for any other solutions that may present themselves.
Also, if you find more than one solution that works, and processing speed is important to you, you can compare them with Benchmark.
|