AutoHotkey Community

It is currently May 24th, 2012, 2:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: April 13th, 2012, 9:39 pm 
Online

Joined: February 6th, 2007, 12:30 am
Posts: 141
Location: Michigan
Finally, a printf function in AutoHotkey that is almost the same as printf functions in other languages.
I impressed myself with this one, but there is still room for improvement to fully implement all functionality as documented here http://www.cplusplus.com/reference/clibrary/cstdio/printf/.

It would be nice to find an elegant way to pass the padchar value (currently defaulted to space) and a way to indicate "allow trunctation" (currently defaulted to no truncation).

As it is, it is very functional...
Feedback encouraged and welcome. :)

Example usage:
Code:
outputdebug DBGVIEWCLEAR   ; view output in dbgview utility from microsoft at www.sysinternals.com

outputdebug % " ID  FirstName  LastName   Number  Code"
outputdebug % "---- ---------- ---------- ------- ----"
outputdebug % printf("%-4s %10s %10s %7f %4s", 23, "Alan", "Lilly", 3.999, "A")
outputdebug % printf("%-4s %10s %10s %-7.3f %4s this is amazing", 23, "Joe", "Apples", 3.999, "B")
outputdebug % printf("%-4s %10s %10s %07.0f %4s", 23, "Thomas", "Sterling", 3.999, "C")

Example formatting numbers:

outputdebug % printf("%07.1f", 145.09)   ; round 1 decimal place and zerofill 7 digits

exitapp

Library Function printf:
Code:
;============================================================
; ahk printf function:
;
; closely emulates perl and c++ printf
; currently supports string and float
;
; string format:
;    - to right justify
;
; float format:
;    - for right justify
;    0 to indicate zerofill
;
; currently the pad character is hard coded as a space until I can find a more elegant way to pass it in
; also truncation does not occur (it would be nice to make this an option)
;============================================================

printf(string, prms*)    ; uses variadics to handle variable number of inputs
{
    padchar := " "
   
    for each, prm in prms
    {
        RegExMatch(string,"`%(.*?)([s|f])",m)
       
        format := m1
        type := m2
       
        if (type = "f") {    ; format float using setformat command
           
            originalformat := A_FormatFloat
            SetFormat, Float, %format%
            prm += 0.0
            SetFormat, Float, %originalformat%
           
        } else if (type = "s") {   ; format string (pad string if necessary, negative number indicates right justify)
       
            if (format < 0)
                loop % -format-StrLen(prm)
                    prm := padchar prm
            else
                loop % format-StrLen(prm)
                    prm := prm padchar
                   
        } else {
            msgbox, unknown type = %type% specified in call to printf
        }
       
        StringReplace, string, string, % "`" m, % prm     ; "%" symbol must be escaped with backtick
       
    }
       
    return string
}

_________________
http://www.panofish.net


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 9th, 2012, 11:43 pm 
Online

Joined: February 6th, 2007, 12:30 am
Posts: 141
Location: Michigan
Minor bugfix for properly handling integers as floats so that zero filling works properly with integers.

_________________
http://www.panofish.net


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], maraskan_user, RaptorX and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group