in reply to Changing the numerical value from 0 to 1

If you find word boundaries a bit hard to rely on (ie, like me, probably don't understand all the possible implications) you probably should use the more robust regex (Kenosis's 2nd example]) to dodge the bullet in some of the cases in this code (Updated to include both of Kenosis' other suggestions):
#!/usr/bin/perl use 5.016; use warnings; # 1071410 change ONLY numbers which are (one digit only) 0 my @arr = qw(20 14 007 0 1203 '' 7900130 43.02 0.03 0.0 ); for my $elem(@arr) { if ( $elem=~ /^0{1}$/ ){ say $elem; my $elem1 = $elem =~ s/^0{1}$/1/; say $elem1; } } say "above, results from Ln1-14; below: Kenosis' 3: \n"; my @arr3 = qw(20 14 007 0 1203 '' 7900130 43.02 0.03 0.0 ); for (@arr3) { print "Original \@arr3 element: |$_| becomes (or remains as) "; $_ = 1 if $_ == 0; say $_; } say "\n Kenosis' 1 (boundary version): "; my @arr1 = qw(20 14 007 0 1203 '' 7900130 43.02 0.03 0.0 ); for (@arr1) { s/\b0\b/1/; print "Original \@arr1 element: |$_| becomes (or remains as) "; say $_; }

Execution:

C:\> 1071410.pl 0 1 above, results from Ln1-14; below: Kenosis' 3: Original @arr3 element: |20| becomes (or remains as) 20 Original @arr3 element: |14| becomes (or remains as) 14 Original @arr3 element: |007| becomes (or remains as) 007 Original @arr3 element: |0| becomes (or remains as) 1 Original @arr3 element: |1203| becomes (or remains as) 1203 Argument "''" isn't numeric in numeric eq (==) at 1071410.pl line 20. Original @arr3 element: |''| becomes (or remains as) 1 Original @arr3 element: |7900130| becomes (or remains as) 7900130 Original @arr3 element: |43.02| becomes (or remains as) 43.02 Original @arr3 element: |0.03| becomes (or remains as) 0.03 Original @arr3 element: |0.0| becomes (or remains as) 1 Kenosis' 1 (boundary version): Original @arr1 element: |20| becomes (or remains as) 20 Original @arr1 element: |14| becomes (or remains as) 14 Original @arr1 element: |007| becomes (or remains as) 007 Original @arr1 element: |1| becomes (or remains as) 1 Original @arr1 element: |1203| becomes (or remains as) 1203 Original @arr1 element: |''| becomes (or remains as) '' Original @arr1 element: |7900130| becomes (or remains as) 7900130 Original @arr1 element: |43.02| becomes (or remains as) 43.02 Original @arr1 element: |1.03| becomes (or remains as) 1.03 Original @arr1 element: |1.0| becomes (or remains as) 1.0

Anchoring a single zero -- as in the match and the substitution above -- seems to me the clearest way of ensuring you substitute only when the spreadsheet cells holds ONLY a single zero.

CAUTIONARY NOTE: I have not tested this with data from a spreadsheet utilizing formatted color, etc) data... and it still seems likely to fail if the column is formatted with leading zeros or a fixed number of fractional (decimalized) values (for example, if "1/2" were presented as ".50" by formatting the cell to require 2 digits after the decimal point but without requiring a leading "0" before the point).

Come, let us reason together: Spirit of the Monastery

Replies are listed 'Best First'.
Re^2: Changing the numerical value from 0 to 1
by Kenosis (Priest) on Jan 21, 2014 at 20:29 UTC

    You've made some very good points. I now think it would have been best to only offer s/^0$/1/--even though the OP said "Word boundaries work !", evidencing that s/\b0\b/1/ met his/her needs.

    I'm not sure about your array elements. Cells don't contain the 007, 0.03 or 0.0 that you placed into an array (unless, of course, it's text, but the OP seems to suggest that s/he's dealing with numeric values). Those elements are the formatting results of the numeric values 7, .03 and 0, respectively.