#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2

    Trying to add a leading zero


    Hi,
    I'm trying to add a leading zero if found one digit number in a file and having a problem.
    Need little help.
    Thanks, testerv

    File:
    Code:
    1.10;
    1.1
    1.4
    1.5
    1.6
    1.7
    1.8
    1.9
    2.2
    3.3
    4.4
    5.5
    6.6
    7.7
    12.12
    134.456
    Want to have:
    Code:
    01.10; 
    01.01 
    01.04 
    01.05 
    01.06 
    01.07 
    01.08 
    01.09 
    02.02 
    03.03 
    04.04 
    05.05 
    06.06 
    07.07 
    12.12 
    134.456
    I made script :
    Code:
            my $file2 = 'test.txt '  ;   
       open my $file2_fh,'<', $file2 or die"Can't open $file2\n" ;
       
       while (my $line = <$file2_fh>)   
        
            { 
               my $len2 = 2 ;
               chomp $line ;                                                 
           
           my ($ln1,$ln2) = split/\./, $line  ;
    
            if (length($ln1) < $len2 ) 
            {   
                 $ln1 = sprintf("%02d", $ln1);      
            }
    
    
            if (length($ln2) < $len2) 
            {   
                 $ln2 = sprintf("%02d", $ln2); 
                        
            }
    
                                      print "$ln1.$ln2 \n"  ;                                      
            }
      exit ;
    Output from the script:
    Code:
    01.10; 
    01.1; 
    01.4; 
    01.5; 
    01.6; 
    01.7; 
    01.8; 
    01.9; 
    02.02 
    03.03 
    04.04 
    05.05 
    06.06 
    07.07 
    12.12 
    134.456
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    The spaces at the end of the line are being counted in the string length. You could trim them off $ln2 with something like s/\s//g.

    Or you could just remove the if()s since a two or more digit number will be unchanged by the %02d formatting (format codes are a minimum length; the entire value will always be displayed).

    I would suggest only dealing with the parts of the string that matter: s/(\d+)/sprintf '%02d', $1/ge and leave anything non-numeric untouched.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    Thanks man I'll try it!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    25
    Rep Power
    0
    What I think you can do is, use a regex to pick out the values for each line, test to see if it is less than 10, if so, add a 0 in the front of the value. The example below does that using your dataset like so:
    Code:
    use warnings;
    use strict;
    
    my $re = qr/([0-9]+)\.([0-9]+)/;
    
    while (<DATA>) {
        if ( my @n = /$re/ ) {
            $n[0] = $n[0] < 10 ? sprintf "0%d", $n[0] : $n[0];
            $n[1] = $n[1] < 10 ? sprintf "0%d", $n[1] : $n[1];
            print join( '.' => @n ), ';', $/;
        }
    }
    
    __DATA__
    1.10;
    1.1
    1.4
    1.5
    1.6
    1.7
    1.8
    1.9
    2.2
    3.3
    4.4
    5.5
    6.6
    7.7
    12.12
    134.456
    Output...
    Code:
    01.10;
    01.01;
    01.04;
    01.05;
    01.06;
    01.07;
    01.08;
    01.09;
    02.02;
    03.03;
    04.04;
    05.05;
    06.06;
    07.07;
    12.12;
    134.456;
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    It works. Thanks Omega!

    Code:
            my $file2 = 'test.txt '  ;   
       open my $file2_fh,'<', $file2 or die"Can't open $file2\n" ;
       
       while (my $line = <$file2_fh>)   
        
            { 
               chomp $line ;                                                 
          if ( $line =~ s/(\d+)/sprintf '%02d', $1/ge )
          {
           print "$line \n"  ;
          }
        }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    779
    Rep Power
    495
    Originally Posted by OmegaZero
    I would suggest only dealing with the parts of the string that matter: s/(\d+)/sprintf '%02d', $1/ge and leave anything non-numeric untouched.
    I think this is probably the best solution.

    Just for the fun of it, this is another possibility demonstrated on the Perl debugger:

    Code:
      DB<1> @d =  map { $c = [split /\./]; join ".", sprintf ("%02d", $c->[0]), $c->[1]} qw / 1.10 12.3/
    
      DB<2> x @d
    0  01.10
    1  12.3
    But OmegaZero's solution is most probably better.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    79
    Rep Power
    2
    Thank you guys for your help!
    testerV

IMN logo majestic logo threadwatch logo seochat tools logo