#!/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).
In reply to Re: Changing the numerical value from 0 to 1
by ww
in thread Changing the numerical value from 0 to 1
by hellohello1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |