AutoHotkey Community

It is currently May 27th, 2012, 7:12 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 166 posts ]  Go to page 1, 2, 3, 4, 5 ... 12  Next
Author Message
PostPosted: March 18th, 2006, 10:59 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear Friends, :)

I am posting here again some of the Foxpro type String manipulation commands that I regularily use with AHK`s FileAppend.
The original post is @ Ask-For-Help Topic: Printing an ASCII formatted text file in Windows.

Quote:
    Functions Supplementing FileAppend

    SetWidth() : SetWidth increases a String's length by adding spaces to it and aligns it Left/Center/Right. ( Requires Space() )
    Code:
    SetWidth(Str,Width,AlignText)
    {
    If (AlignText!=0 and AlignText!=1 and AlignText!=2)
       AlignText=0
    if AlignText=0         
       {
        RetStr= % (Str)Space(Width)
        StringLeft,RetStr,RetStr,%Width%
       }
    If AlignText=1         
       {
        Spaces:=(Width-(StrLen(Str)))
        RetStr= % Space(Round(Spaces/2))(Str)Space(Spaces-(Round(Spaces/2)))
       }
    if AlignText=2         
       {
        RetStr= % Space(Width)(Str)
        StringRight,RetStr,RetStr,%Width%
       }
    Return RetStr
    }

    SetWidth() supplements AHK's FileAppend() in creating well formatted Text Files. Try windir.ahk, which is a demo script that lists "Windows folder contents" (in a tabular format) to windir.txt

    NumStr()
    Code:
    NumStr( Value, Width, Dec, PadB4="" )   {
      AFF := A_FormatFloat
      SetFormat, Float, 0.%Dec%
      Value += 0.00
      Loop
           If (StrLen(Value) < Width AND PadB4!="")
              Value := PadB4 Value
           else
              Break
      SetFormat, Float, %AFF%
      Return Value
    }

    Use NumStr() to format a float . You may also use NumStr() to pad leading characters (any character!) to a numeric string.

    Examples: Number=21.3263
    NumStr(Number,10,2,"_") returns "_____21.33"
    NumStr(Number,10,0,"0") returns "0000000021"
    NumStr(Number,10,2) returns "21.33" (Note: 21.33 is leaded by 5 spaces which I am not able to reproduce)
    FileAppend, % "ItemCode" NumStr(Number,5,0,0) , Temp.txt outputs "ItemCode00021" to temp.txt

    Try the Demo DiskStat.ahk or view its output DiskStat.txt. This demo uses NumStr() and SetWidth()

    Edit: 2007-11-22
    I use this this Shorter version now:

    Code:
    NumFormat( Number=0, Width=3, Decimal=0,  PadChar="" ) {
      VarSetCapacity( Padding, Width, Asc( PadChar ) )
      OutLen := StrLen( Number := Round( Number, Decimal ) )
    Return SubStr( Padding . Number , 1 - ( OutLen >= Width ? Outlen : Width  ) )
    }



    Replicate()
    Code:
    Replicate( Chr=" ", X=1 ) {
     Return VarSetCapacity( V, VarSetCapacity(V,VarSetCapacity(V,64)>>32)+X, Asc(Chr) ) ? V :
    }

    Example: This creates a horizontal ruling 80 characters wide
    FileAppend, % replicate(chr(196),80), filename.txt

    Space()
    Code:
    Space(Width)
    {
    Loop,%Width%
    Space=% Space Chr(32)
    Return Space
    }

    Quote:
    This is simpler:
    FileAppend,% "Hello" Space(3) "World",filename.txt
    rather than
    FileAppend,Hello%A_Space%%A_Space%%A_Space%World",filename.txt

    SubStr() : AHK's StringMid as a Function
    Code:
    Substr(String,SPos,Chars)
    {
    StringMid,String,String,%SPos%,%Chars%
    Return String
    }

    String Case : Convert a string to UPPER / lower / Proper (Sentence) case.
    Code:
    UPPER(String)
    {
    StringUpper,String,String
    Return String
    }

    Code:
    LOWER(String)
    {
    StringLower,String,String
    Return String
    }

    Code:
    PROPER(String)
    {
    StringLower,String,String,T
    Return String
    }

    Date Functions : These functions manipulate AHK's FormatTime ... FormatTime() would suffice all needs & other date functions are Superfluous & available with Foxpro
    Code:
    FormatTime(TimeString,Format)
    {
    FormatTime, FormattedTime , TimeString, %Format%
    return Formattedtime
    }

    Code:
    CDOW(DATETIME)
    {
    FormatTime,CDOW,DATETIME,dddd
    Return CDOW
    }

    Code:
    CMONTH(DATETIME)
    {
    FormatTime,CMONTH,DATETIME,MMMM
    Return CMONTH
    }

    Code:
    DATE(DATETIME)
    {
    FormatTime,Date,DATETIME,dd-MM-yyyy
    Return Date
    }

    Code:
    TIME(DATETIME)
    {
    FormatTime,Time,DATETIME,hh:mm:ss tt
    Return Time
    }

    Code:
    TIME24(DATETIME)
    {
    FormatTime,Time,DATETIME,HH:mm:ss
    Return Time
    }

    Code:
    DOW(DATETIME)
    {
    FormatTime,DOW,DATETIME,WDay
    Return DOW
    }


Thanks for reading this patiently.......

Edit: 2006/02/03 - Added code for function "Replicate()"
Edit: 2006/02/05 - Added code for function "Space()"
Edit: 2006/02/06 - Added code for function "SetWidth()" / Download the demo windir.ahk or view sample output windir.txt
Edit: 2006/02/12 - Added code for function "SubStr()"
Edit: 2006/02/18 - Added code for FormatTime() and other Superfluous date functions
Edit: 2006/02/19 - Added code for Upper() Lower() Proper()
Edit: 2006/02/20 - Added code for function "NumStr()" / Download the demo DiskStat.ahk or view sample output DiskStat.txt


Thank you all. Regards, :)


Last edited by SKAN on August 9th, 2011, 9:15 pm, edited 46 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2006, 1:53 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Nice set of functions. I'm sure these will be useful to quite a few people. Also, they may give me some ideas for new functions to distribute with the program, either built-in or in external files included via #include.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2006, 9:36 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
:D :D :D
Thank you very much Mr.Chris Mallet
Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 12:28 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Quote:
Function LDOM()

Derives & Returns the Last Day Of Month

Examples:

TotalDays := LDOM() ; uses System date
TotalDays := LDOM("20040219") ; returns 29 as 2004 is a leap year

Code:
LDOM(TimeStr="")
 {
  If TimeStr=
     TimeStr:=A_Now

  StringLeft,Date,TimeStr,6
  Day  = 28
  Date = %Date%%Day%
  FormatTime,cMonth,%Date%,M

  Loop,3
    {
      Date+=1,days
      FormatTime,tMonth,%Date%,M
       if tMonth != %cMonth%
          break
       else
          Day+=1
    }
  Return Day
 }


I will be glad if this code is optimised..

Regards :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 5:26 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Goyyah wrote:
I will be glad if this code is optimised..

Code:
MsgBox % LDOM(200601)
MsgBox % LDOM(200602)
MsgBox % LDOM(200612)
MsgBox % LDOM(200002)

LDOM(TimeStr="") {
  If TimeStr=
     TimeStr = %A_Now%
  StringLeft Date,TimeStr,6 ; YearMonth
  Date1 = %Date%
  Date1+= 31,D              ; A day in next month
  StringLeft Date1,Date1,6  ; YearNextmonth
  Date1-= %Date%,D          ; Difference in days
  Return Date1
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 6:50 pm 
Offline

Joined: August 26th, 2004, 3:11 pm
Posts: 80
Location: Chelsea - MA, USA
Quote:
TotalDays := LDOM("20040219") ; returns 29 as 2004 is a leap year
Wrong value, should be "20040229".

_________________
Working now on:
NumpadMouse v2 (Draw)
top-recode project (private server script) (pkodev)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 6:54 pm 
deguix wrote:
Wrong value, should be "20040229".
So that means you already knows the las day oft the month.
You don't need the function.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 7:11 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you want YearMonthDay format to be returned, replace the return instruction with
Code:
  Return Date Date1


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 7:16 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you want nice, formatted date to be returned, replace the Return instruction in my script with 2 lines, similar to these
Code:
  FormatTime Date1, %Date%%Date1%, MM/dd/yy
  Return Date1
The date format string could be given to the function as an optional second parameter, so you could get the last day of the month in many different formats.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 9:28 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Laszlo wrote:
Goyyah wrote:
I will be glad if this code is optimised..

Code:
LDOM(TimeStr="") {
  If TimeStr=
     TimeStr = %A_Now%
  StringLeft Date,TimeStr,6 ; YearMonth
  Date1 = %Date%
  Date1+= 31,D              ; A day in next month
  StringLeft Date1,Date1,6  ; YearNextmonth
  Date1-= %Date%,D          ; Difference in days
  Return Date1
}


Dear Laszlo, :D

Brilliant! You always amaze me .. I would have never thought this way.
I wrote LDOM() 10 years before (in Foxpro) for a Payroll Software.
What I have presented here is a optimised version. ( I thought so! )

In the original function, I start with day 1 (for the given date) and
loop till the month changes and then break.. :shock:

I have to change the way I envision, I guess! :D

Thank you .. Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2006, 9:42 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear deguix, :)

deguix wrote:
Quote:
TotalDays := LDOM("20040219") ; returns 29 as 2004 is a leap year
Wrong value, should be "20040229".


To clarify: I have titled the function as "Last Day of Month" and not "Last Date of Month"

I rewrote the function in AHK after this request @ Ask for Help Forum section : Total number of the current month
But you have given a good idea & Laszlo has already replied with a solution. I guess the function should accept a second parameter & return the value accordingly.

Thank you. Regards :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 29th, 2006, 6:41 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
I wonder why EnvAdd/Sub doesn't accept to add/substract months or even years. In Visual Basic (Excel, etc.), I used to go to the first day of the next month, then go back one day. An old, well known trick.

Well, Laszlo's trick works well too. :-)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2006, 8:40 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear Friends, :)

The following three functions play an important role in my AHK ColorPicker Project posted
@ http://www.autohotkey.com/forum/viewtopic.php?p=57968#57968

Code:
HEX2RGB(HEXString,Delimiter="")
{
 If Delimiter=
    Delimiter=,
 
 StringMid,R,HexString,1,2
 StringMid,G,HexString,3,2
 StringMid,B,HexString,5,2

 R = % "0x"R
 G = % "0x"G
 B = % "0x"B
 
 R+=0
 G+=0
 B+=0

 RGBString = % R Delimiter G Delimiter B

Return RGBString
}

Code:
RGB2HEX(RGBString,Delimiter="")
{
 If Delimiter=
    Delimiter=,
 StringSplit,_RGB,RGBString,%Delimiter%

 SetFormat, Integer, Hex
 _RGB1+=0
 _RGB2+=0
 _RGB3+=0

 If StrLen(_RGB1) = 3
    _RGB1= 0%_RGB1%

 If StrLen(_RGB2) = 3
    _RGB2= 0%_RGB2%

 If StrLen(_RGB3) = 3
    _RGB3= 0%_RGB3%

 SetFormat, Integer, D
 HEXString = % _RGB1 _RGB2 _RGB3
 StringReplace, HEXString, HEXString,0x,,All
 StringUpper, HEXString, HEXString

Return, HEXString
}

Code:
CheckHexC(HEXString)
{
  StringUpper, HEXString, HEXString

  RGB:=HEX2RGB(HEXString)
  CHK:=RGB2HEX(RGB)

  StringUpper, CHK, CHK

  If CHK=%HEXString%
     Return 1
  else
     Return 0
}


Quote:
    Examples:

    HEX2RGB("FFFFFF") returns "255,255,255"
    RGB2HEX("255,255,255") returns "FFFFFF"

    Function CheckHexC() validates a "Hex Color Code" by calling the above two functions.

    CheckHexC("FFFFFF") returns 1 (a logical true)
    CheckHexC("GOYYAH") returns 0 (a logical false)


These functions work okay, but I will feel glad if they are optimised.

Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Last edited by SKAN on April 28th, 2006, 11:03 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2006, 10:47 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Dear Goyyah
I suppose you use Quote to get a reliable clear background, but I find it quite disturbing, as one searches where your own message starts...

Anyway, here is my take at these functions:
Code:
Hex2RGB(_hexRGB, _delimiter="")
{
   local color, r, g, b, decimalRGB

   If _delimiter =
      _delimiter = ,
   color += "0x" . _hexRGB
   b := color & 0xFF
   g := (color & 0xFF00) >> 8
   r := (color & 0xFF0000) >> 16
   decimalRGB := r _delimiter g _delimiter b
   Return decimalRGB
}

RGB2Hex(_decimalRGB, _delimiter="")
{
   local weight, color, hexRGB

   If _delimiter =
      _delimiter = ,
   weight = 16
   SetFormat Integer, Hex
   color := 0x1000000
   Loop Parse, _decimalRGB, %_delimiter%
   {
      color += A_LoopField << weight
      weight -= 8
   }
   StringTrimLeft hexRGB, color, 3
   SetFormat Integer, D
   Return hexRGB
}

CheckHexColor(_hexRGB)
{
   If (StrLen(_hexRGB) != 6)
      Return false
   Loop Parse, _hexRGB
   {
      If A_LoopField not in 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
         Return false
   }
   Return true
}

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on April 29th, 2006, 7:29 am, edited 4 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2006, 11:02 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Daer PhiLho, :)

You wrote:
I suppose you use Quote to get a reliable clear background, but I find it quite disturbing, as one searches where your own message starts...

Sorry! My intention was to present it clearily.. sometimes I overdo things :(.
I have edited the post.

:D Thanks a lot for your optimised version of the functions! :D

Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Last edited by SKAN on April 28th, 2006, 1:27 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 166 posts ]  Go to page 1, 2, 3, 4, 5 ... 12  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Stigg and 21 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