Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AHK Functions :: InCache() - Cache List of Recent Items


  • Please log in to reply
172 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
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.

Functions Supplementing FileAppend

SetWidth() : SetWidth increases a String's length by adding spaces to it and aligns it Left/Center/Right. ( Requires Space() )

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()
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:

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()
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()
Space(Width)
{
Loop,%Width%
Space=% Space Chr(32)
Return Space
}

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
Substr(String,SPos,Chars)
{
StringMid,String,String,%SPos%,%Chars%
Return String
}
String Case : Convert a string to UPPER / lower / Proper (Sentence) case.
UPPER(String)
{
StringUpper,String,String
Return String
}
LOWER(String)
{
StringLower,String,String
Return String
}
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
FormatTime(TimeString,Format)
{
FormatTime, FormattedTime , TimeString, %Format% 
return Formattedtime
}
CDOW(DATETIME)
{
FormatTime,CDOW,DATETIME,dddd
Return CDOW
}
CMONTH(DATETIME)
{
FormatTime,CMONTH,DATETIME,MMMM
Return CMONTH
}
DATE(DATETIME)
{
FormatTime,Date,DATETIME,dd-MM-yyyy
Return Date
}
TIME(DATETIME)
{
FormatTime,Time,DATETIME,hh:mm:ss tt
Return Time
}
TIME24(DATETIME)
{
FormatTime,Time,DATETIME,HH:mm:ss
Return Time
}
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, :)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
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.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
:D :D :D
Thank you very much Mr.Chris Mallet
Regards, :)
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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

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 :)
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

I will be glad if this code is optimised..

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
}


deguix
  • Members
  • 87 posts
  • Last active: Jun 17 2014 05:18 AM
  • Joined: 26 Aug 2004

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

Wrong value, should be "20040229".

April
  • Guests
  • Last active:
  • Joined: --

Wrong value, should be "20040229".

So that means you already knows the las day oft the month.
You don't need the function.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
If you want YearMonthDay format to be returned, replace the return instruction with
Return Date Date1


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
If you want nice, formatted date to be returned, replace the Return instruction in my script with 2 lines, similar to these
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.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I will be glad if this code is optimised..

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, :)
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear deguix, :)

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 :)
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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. :-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Friends, :)

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

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
}
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
}
CheckHexC(HEXString)
{
  StringUpper, HEXString, HEXString

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

  StringUpper, CHK, CHK

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

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, :)
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
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:
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
}

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

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Daer PhiLho, :)

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, :)
kWo4Lk1.png