这是一个有点晦涩的问题,但是在 Scala 的字符串插值中循环的最佳方法是什么?例如,如果你想这样做
html"""<ul>
${
for (todoItem <- todoList) {
html"""<li>TODO: ${todoItem}</li>"""
}
}
</ul>"""
Run Code Online (Sandbox Code Playgroud)
我看不到累积内部 html 类的简洁方法,以便字符串插值器可以使用它。我唯一能想到的是
html"""<ul>
${
var htmlList=List.empty[Html]
for (todoItem <- todoList) {
htmlList :+ html"""<li>TODO: ${todoItem}</li>"""
}
htmlList
}
</ul>"""
Run Code Online (Sandbox Code Playgroud)
并在我的自定义 html 插值器中添加对它的支持
在下面的工作表中,我创建了一个自定义字符串插值器.
object WSLookup {
implicit class LookupSC(val sc: StringContext) extends AnyVal {
def lookup(args: Any*): String = {
val strings = sc.parts.iterator
val expressions = args.iterator
var buf = new StringBuffer(strings.next)
while (strings.hasNext) {
buf append doLookup(expressions.next.toString)
buf append strings.next
}
buf.toString()
}
def doLookup(s: String): String = {
// Just change the string to uppercase to test.
s.toUpperCase
}
}
val x = "cool"
val testString = "Not $x"
lookup"How $x"
// lookup testString //<--- See question 1
} …Run Code Online (Sandbox Code Playgroud) 据我所知(在 GitHub 中浏览流行的 PHP 代码)有很多人没有使用字符串插值:
$loader->load($config['resource'].'.xml');
Run Code Online (Sandbox Code Playgroud)
相对:
$loader->load("{$config['resource']}.xml");
Run Code Online (Sandbox Code Playgroud)
不使用字符串插值是否有任何原因(即性能)?
我想调用“ myActionID ”变量的值。我怎么做?如果我将诸如“ actionId”:1368201之类的静态值传递给myActionID,则它可以工作,但是如果我使用“ actionId”:$ {actionIdd},它将给出错误。
以下是相关代码:
class LaunchWorkflow_Act extends Simulation {
val scenarioRepeatCount = 1
val userCount = 1
val myActionID = "13682002351"
val scn = scenario("LaunchMyFile")
.repeat (scenarioRepeatCount) {
exec(session => session.set("counter", (globalVar.getAndIncrement+" "+timeStamp.toString())))
.exec(http("LaunchRequest")
.post("""/api/test""")
.headers(headers_0)
.body(StringBody(
"""{ "actionId": ${myActionID} ,
"jConfig": "{\"wflow\":[{\"Wflow\":{\"id\": \"13500145349\"},\"inherit-variables\": true,\"workflow-context-variable\": [{\"variable-name\": \"externalFilePath\",\"variable-value\": \"/var/nem/nem/media/mount/assets/Test.mp4\"},{\"variable-name\": \"Name\",\"variable-value\": \"${counter}\"}]}]}"
}""")))
.pause(pause)
}
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
Run Code Online (Sandbox Code Playgroud)
如果我将值13682002351代替myActionID,则一切正常。先谢谢了。在Gatling中执行此脚本时,出现此错误
错误ighttp.action.HttpRequestAction-'httpRequest-3'执行失败:未定义名为'myActionID'的属性
假设我使用sigil_S以下方法构造一个字符串
iex> s = ~S(#{1 + 1})
"\#{1 + 1}"
Run Code Online (Sandbox Code Playgroud)
然后我如何让Elixir评估该字符串或执行插值,就像我输入了字面值一样"#{1 + 1}"?
换句话说,我该如何评价"2"呢?
我知道我可以使用EEx(例如EEx.eval_string "<%=1 + 1%>"),但我很好奇是否有办法只使用'普通'字符串插值.
所以我试图为我的脚本编写一个小的配置文件,它应该指定一个 IP 地址、一个端口和一个 URL,这些文件应该通过使用前者到变量的插值来创建。我的config.ini看起来像这样:
[Client]
recv_url : http://%(recv_host):%(recv_port)/rpm_list/api/
recv_host = 172.28.128.5
recv_port = 5000
column_list = Name,Version,Build_Date,Host,Release,Architecture,Install_Date,Group,Size,License,Signature,Source_RPM,Build_Host,Relocations,Packager,Vendor,URL,Summary
Run Code Online (Sandbox Code Playgroud)
在我的脚本中,我解析这个配置文件如下:
config = SafeConfigParser()
config.read('config.ini')
column_list = config.get('Client', 'column_list').split(',')
URL = config.get('Client', 'recv_url')
Run Code Online (Sandbox Code Playgroud)
如果我运行我的脚本,这会导致:
config = SafeConfigParser()
config.read('config.ini')
column_list = config.get('Client', 'column_list').split(',')
URL = config.get('Client', 'recv_url')
Run Code Online (Sandbox Code Playgroud)
我试过调试,结果又给了我一行错误代码:
Traceback (most recent call last):
File "server_side_agent.py", line 56, in <module>
URL = config.get('Client', 'recv_url')
File "/usr/lib64/python2.7/ConfigParser.py", line 623, in get
return self._interpolate(section, option, value, d)
File "/usr/lib64/python2.7/ConfigParser.py", line 691, in _interpolate
self._interpolate_some(option, L, …Run Code Online (Sandbox Code Playgroud) 我是 Scala 的新手,所以请随时为我指出文档的方向,但我无法在我的研究中找到这个问题的答案。
我将scala 2.11.8 与 Spark2.2 一起使用,并尝试使用插值创建一个包含 dateString1_dateString2(带下划线)的动态字符串,但存在一些问题。
val startDt = "20180405"
val endDt = "20180505"
Run Code Online (Sandbox Code Playgroud)
这似乎有效:
s"$startDt$endDt"
res62: String = 2018040520180505
Run Code Online (Sandbox Code Playgroud)
但这失败了:
s"$startDt_$endDt"
<console>:27: error: not found: value startDt_
s"$startDt_$endDt"
^
Run Code Online (Sandbox Code Playgroud)
我希望这个带有转义的简单解决方法可以工作,但不会产生预期的结果:
s"$startDt\\_$endDt"
res2: String = 20180405\_20180505
Run Code Online (Sandbox Code Playgroud)
请注意,这个问题不同于Why can't _ be used inside of string interpolation? 因为这个问题希望找到一个可行的字符串插值解决方案,而上一个问题则更侧重于 Scala 内部。
因此,我有一个Jenkins Pipeline,它使用Jenkins Pipeline提供的readFile方法读取文本文件(JSON)。文本文件app.JSON具有多个变量,这些变量已在Jenkins管道中定义。
虽然readFile确实读取了文件并将其转换为字符串,但它不会插值这些变量。除了简单的字符串替换外,我还有哪些插值这些变量的选项(我想避免)
我知道我可以使用readJSON或JSON解析器,但我希望将输出以字符串形式显示,这样我可以更轻松地将其作为字符串读取并传递。
我尝试使用Gstrings,$ {-> variable}和.toString()方法。什么都没有为我工作。
詹金斯管道规范
appServerName = 'gaga'
def appMachine = readFile file: 'util-silo-create-v2/app.json'
println appMachine
Run Code Online (Sandbox Code Playgroud)
app.json
{
"name":"${appServerName}",
"fqdn":"${appServerName}"
}
Run Code Online (Sandbox Code Playgroud)
我想替换的管道和app.json中都有多个变量
The issue is with the readFile method provided by Jenkins Pipeline. Although it is very neat and easy to use it does not interpolate strings.
I expect below output
println appMachine
{
"name":"gaga",
"fqdn":"gaga"
}
Run Code Online (Sandbox Code Playgroud)
Output I am getting
{
"name":"${appServerName}",
"fqdn":"${appServerName}"
}
Run Code Online (Sandbox Code Playgroud) I have an input string for my Golang CLI tool with some references to environment variables in bash syntax ($VAR and ${VAR}), e.g.:
$HOME/somedir/${SOME_VARIABLE}dir/anotherdir-${ANOTHER_VARIABLE}
Run Code Online (Sandbox Code Playgroud)
What is the most efficient way to interpolate this string by replacing environemnt variables references with actual values? For previous example it can be:
/home/user/somedir/4dir/anotherdir-5
Run Code Online (Sandbox Code Playgroud)
if HOME=/home/user, SOME_VARIABLE=4 and ANOTHER_VARIABLE=5.
Right now I'm using something like:
func interpolate(str string) string {
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", …Run Code Online (Sandbox Code Playgroud) 我有两个目录,每个目录都有一个文件:
$ ls -l "test dir["
-rw-r--r-- 1 root media 0B 11 Dec 16:53 .ignoreme
Run Code Online (Sandbox Code Playgroud)
和
$ ls -l "test [dir]"
-rw-r--r-- 1 root media 0B 11 Dec 16:34 .ignoreme
Run Code Online (Sandbox Code Playgroud)
我正在使用 Perl 来测试“.ignoreme”文件是否存在:
$ perl -e '$d="test dir["; print -e glob qq("$d"/.ignore*); print "\n";'
1
Run Code Online (Sandbox Code Playgroud)
哪个有效,以及
$ perl -e '$d="test [dir]"; print -e glob qq("$d"/.ignore*); print "\n";'
Run Code Online (Sandbox Code Playgroud)
哪个没有。
我猜这与这[]对有关,但我不确定相互作用。有人可以请教我。我也很感谢修复以匹配任何$d包含[].
scala ×4
string ×2
configparser ×1
elixir ×1
gatling ×1
glob ×1
go ×1
groovy ×1
java ×1
perl ×1
php ×1
python ×1
scala-2.10 ×1
scala-2.11 ×1