 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Sat Mar 18, 2006 10:59 am Post subject: AHK Functions - RandStr() |
|
|
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)
{
Loop,%x%
String=%String%%chr%
return string
} |
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,  _________________ SKAN - Suresh Kumar A N
Last edited by SKAN on Tue Sep 02, 2008 2:57 pm; edited 23 times in total |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Sun Mar 19, 2006 1:53 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Sun Mar 19, 2006 9:36 am Post subject: |
|
|
Thank you very much Mr.Chris Mallet
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Tue Mar 28, 2006 12:28 pm Post subject: |
|
|
| 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  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4015 Location: Pittsburgh
|
Posted: Tue Mar 28, 2006 5:26 pm Post subject: |
|
|
| 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
} |
|
|
| Back to top |
|
 |
deguix
Joined: 26 Aug 2004 Posts: 72 Location: Everett - MA, USA
|
Posted: Tue Mar 28, 2006 6:50 pm Post subject: |
|
|
| Quote: | | TotalDays := LDOM("20040219") ; returns 29 as 2004 is a leap year | Wrong value, should be "20040229". _________________ Working now on:
NSIS Wiki (NSIS) |
|
| Back to top |
|
 |
April Guest
|
Posted: Tue Mar 28, 2006 6:54 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4015 Location: Pittsburgh
|
Posted: Tue Mar 28, 2006 7:11 pm Post subject: |
|
|
| If you want YearMonthDay format to be returned, replace the return instruction with |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4015 Location: Pittsburgh
|
Posted: Tue Mar 28, 2006 7:16 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Tue Mar 28, 2006 9:28 pm Post subject: |
|
|
| 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,
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..
I have to change the way I envision, I guess!
Thank you .. Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Tue Mar 28, 2006 9:42 pm Post subject: |
|
|
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  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Mar 29, 2006 6:41 am Post subject: |
|
|
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.  _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Thu Apr 27, 2006 8:40 pm Post subject: |
|
|
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,  _________________ SKAN - Suresh Kumar A N
Last edited by SKAN on Fri Apr 28, 2006 11:03 am; edited 2 times in total |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Fri Apr 28, 2006 10:47 am Post subject: |
|
|
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
}
|
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Last edited by PhiLho on Sat Apr 29, 2006 7:29 am; edited 4 times in total |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5884
|
Posted: Fri Apr 28, 2006 11:02 am Post subject: |
|
|
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.
Thanks a lot for your optimised version of the functions!
Regards,  _________________ SKAN - Suresh Kumar A N
Last edited by SKAN on Fri Apr 28, 2006 1:27 pm; edited 1 time in total |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|