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:

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.

-- Ken


In reply to Re: Changing the numerical value from 0 to 1 by kcott
in thread Changing the numerical value from 0 to 1 by hellohello1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.