Jump to content

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

Variable within string variable


  • Please log in to reply
8 replies to this topic
okram
  • Members
  • 230 posts
  • Last active: Oct 15 2015 05:55 AM
  • Joined: 05 Oct 2012

Hi, I have a given variable within a string variable. How to show this string to get Today is Saturday text?

date:="Today is %A_DDDD%"
msgbox %date%  ;shows Today is %A_DDDD%
msgbox % %date% ;returns error


Leo Xiong
  • Members
  • 140 posts
  • Last active: Mar 15 2014 06:42 AM
  • Joined: 13 Apr 2010

Read this.

date := "Today is " A_DDD
MsgBox, %date%

gplus-16.pngLeo Xiong.com
#ahk freenode - catch me there as NameLess-exe!

okram
  • Members
  • 230 posts
  • Last active: Oct 15 2015 05:55 AM
  • Joined: 05 Oct 2012

Thanks but in my case date variable is given and cannot be changed. Is there a way to use a date variable as it is (variable in string variable)?



Leo Xiong
  • Members
  • 140 posts
  • Last active: Mar 15 2014 06:42 AM
  • Joined: 13 Apr 2010

Try

date := "%A_DD%/%A_MMM%/%A_YYYY%"
StringReplace, date, date, `%A_DDDD`%, %A_DDDD%, All
MsgBox, % date

or use what tidbit (on the IRC) said

transform, date, Deref, %date% 

Or this function that I wrote to replace all variables in a string with it's actual value. Use with caution.

date := "%A_DD%/%A_MMM%/%A_YYYY%"

MsgBox, % evalVariablesInString(date)

evalVariablesInString(string){
	pos := 0
	while (pos := RegExMatch(string, "%(.*?)%", regex, ++pos += StrLen(%regex1%)))
		string := RegExReplace(string, regex, %regex1%)
	return string
}

gplus-16.pngLeo Xiong.com
#ahk freenode - catch me there as NameLess-exe!

okram
  • Members
  • 230 posts
  • Last active: Oct 15 2015 05:55 AM
  • Joined: 05 Oct 2012

Thank you, both functions work very well.

 

I understand the only way then is to manipulate a string before sending and there is no direct way.



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
Did you try this code

.

date := "Today is " A_DDDD
MsgBox, %date%

Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

okram
  • Members
  • 230 posts
  • Last active: Oct 15 2015 05:55 AM
  • Joined: 05 Oct 2012

yes, I explained that my date variable cannot be changed.

date:="Today is %A_DDDD%"


punchin
  • Members
  • 117 posts
  • Last active: Dec 10 2019 10:27 PM
  • Joined: 25 Mar 2012

You're declaring the date variable to be the string "Today is %A_DDDD%". If it absolutely cannot be changed, then you can never have it show the date no matter what %A_DDDD% is set to.

 

Why can't the date variable be changed?



okram
  • Members
  • 230 posts
  • Last active: Oct 15 2015 05:55 AM
  • Joined: 05 Oct 2012

Leo Xiong, thanks, I've seen your update, Transform is great!

transform, date, Deref, %date%