in reply to Changing the numerical value from 0 to 1

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

Replies are listed 'Best First'.
Re^2: Changing the numerical value from 0 to 1
by hellohello1 (Sexton) on Jan 28, 2014 at 03:03 UTC
    Hi, I'm back again. Somehow, the word boundaries doesn't seem to work for some files, which some "0" remains as "0". I have tried
    push(@output5b, $outputb); for (@output5b) { s/^0$/1/};
    But the zero does not change to 1 at all. I am not sure why is that so and whether there might have something that I missed out.

      You'll need to show the data in order for us to provide a meaningful response.

      If you actually print your values, you may be able to see why the substitution isn't working for yourself.

      -- Ken

        Hi Kcott, I think it works this time. I put it at @resultarray instead of @output5b.  for (@resultarray) { s/^0$/1/ }; So far, it seems to work ok, though I am trying to figure out why putting at @output5b doesn't work since I am printing the values from @output5b. Here's my whole code
        for (@resultarray) { s/^0$/1/ }; for(my $i = 0; $i < $originalfilecount; $i++) { if($first) { $first = 0; #print OUT5 "\t$resultarray[3]"; $outputb = "\t$resultarray[3]"; } } #print OUT5 "\n"; push(@output5a, $outputa); push(@output5b, $outputb); for(@output5b){s/0/1/g}; #convert output from 0 to 1
        May I ask how do I substitute the $resultarray3 values from 0 to 1 instead of doing at @resultarray? Thanks for all your help :)