Version 6 of SYStems

Updated 2005-11-07 15:20:04 by suchenwi

The Egyptian wannabe developer/sys-admin


 # Allow spaces and underscores in long number for easy reading
 proc norm {num} {
     if {[regexp {^(\s|\d|_)*(\.)?(\s|_)*(\d)+(\s|\d|_)*$} $num]} {
         string map {_ {} \  {}} $num
     } else {
         error "Number: $num is not in normal form"
     }
 }

 Example:
 expr [norm 10_000_000] + [norm 100._10_100]
 => 10000100.101

AMG: Neat! Here, this proc does the reverse (adds underscores):

 proc abnorm {num} {
     if {![string is double -strict $num]} {
         error "not a number: \"$num\""
     }
     set point [string first . $num]
     if {$point == -1} {
         set point [string length $num]
     } else {
         for {set pos [expr {$point + 4}]} {$pos < [string length $num]} {incr pos 4} {
             set num [string replace $num $pos $pos _[string index $num $pos]]
         }
     }
     for {set pos [expr {$point - 3}]} {$pos > 0} {incr pos -3} {
         set num [string replace $num $pos $pos _[string index $num $pos]]
     }
     return $num
 }

Example:

 % abnorm 100.10100
 100.101_00
 % abnorm 10000000
 10_000_000
 % abnorm 10000100.101
 10_000_100.101

This code doesn't handle exponential notation or even negative numbers. I don't want to put much more effort into it, since I'm more interested in seeing some clever [regsub] version. If anyone wants to give it a try, ... go right ahead. Have a blast.

AMG: I woke this morning with an inspiration about how to do this with [regsub].

 proc sreverse {str} {
    set result ""
    for {set i [expr {[string length $str] - 1}]} {$i >= 0} {incr i -1} {
      append result [string index $str $i]
    }
    return $result
 }

 proc abnorm {num} {
     if {![string is double -strict $num]} {
         error "not a number: \"$num\""
     }
     regexp {^(-|\+)?(\d+)?(\.)?(\d+)?(e|E)?(-|\+)?(\d+)?$} $num num sm int dot frac e se exp
     set result $sm
     append result [regsub -all {(?!^)(?=(\d{3})+$)} $int _]
     append result $dot
     append result [sreverse [regsub -all {(?!^)(?=(\d{3})+$)} [sreverse $frac] _]]
     append result $e$se$exp
     return $result
 }

I don't bother putting _'s in the exponent because we can't have exponents 1000 and up. I have to do the [sreverse] bit because while we may have look-ahead regular expressions, we do not have look-behind.


RS proposes this simpler version of sreverse:

 proc sreverse str {
    set res ""
    set i [string length $str]
    while {$i} {append res [string index $str [incr i -1]]}
    set res
 }
 % sreverse hello
 olleh

Category Person