为什么这个清理函数的输出没有变化?

发布于 2024-10-07 16:38:57 字数 727 浏览 0 评论 0原文

我希望以下函数输出经过清理的标签列表,但它会输出未更改的原始列表。然而,在每次迭代中转储 local.i 都会按预期显示经过清理的标签:

<cfset this.tags = "tag!@%^-tag, anothersf-taggame, SDFSFtagged">

<cfset local.listPosition = 0>

<cfloop list="#this.tags#" index="local.i">
   <cfset local.i = ReReplace(local.i, "[^a-z0-9+##\-.]", "", "All")>
   <cfset local.listPosition = local.listPosition + 1>
   <cfset ListSetAt(this.tags, local.listPosition, local.i, ", ")>
</cfloop>

<cfoutput>
   #this.tags#
</cfoutput>

知道为什么会发生这种奇怪的行为吗?它必须是 ListSetAt() 函数不附加列表中有新值,但为什么呢?

I expect the following function to output a sanitized list of tags, but instead it outputs the original list unchanged. Dumping local.i on each iteration, however, shows the sanitized tag as one would expect:

<cfset this.tags = "tag!@%^-tag, anothersf-taggame, SDFSFtagged">

<cfset local.listPosition = 0>

<cfloop list="#this.tags#" index="local.i">
   <cfset local.i = ReReplace(local.i, "[^a-z0-9+##\-.]", "", "All")>
   <cfset local.listPosition = local.listPosition + 1>
   <cfset ListSetAt(this.tags, local.listPosition, local.i, ", ")>
</cfloop>

<cfoutput>
   #this.tags#
</cfoutput>

Any idea why this weird behavior is happening? It must be the ListSetAt() function not appending the new value to the list, but why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-10-14 16:38:58

listSetAt() 不会更改列表。它返回包含更改的列表副本,因此您需要进行分配。

不过,看起来这里还有很多可用的优化。你为什么要循环遍历这个列表?看起来如果您在正则表达式中允许逗号,您将得到与此循环相同的结果。

listSetAt() doesn't alter the list. It returns a copy of the list with the alteration, so you need to do an assignment.

It looks like there's a lot of optimization available here, though. Why are you looping over the list? It looks like if you allow commas in the regex, you would get the same results as this loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文