【问题标题】:Match variable name with another variable's name将变量名与另一个变量名匹配
【发布时间】:2015-08-11 13:17:22
【问题描述】:

我可以在 perl 中搜索变量名作为正则表达式吗?而不是它的价值。 类似于下面的算法

$var = 0;
$pattern = "abc";

if($i = var)   #here I want to check if "pattern" is "var" or not. and dont want to compare actual values of them like "0" and "abc".
then
    do something
endif

有人可以帮忙吗?

【问题讨论】:

  • 你使用什么语言?
  • 如果您事先知道,我不明白是什么阻止您使用变量的名称。如果不知道变量名,有几种获取方式perlmonks.org/?node_id=1002907stackoverflow.com/questions/747521/…stackoverflow.com/questions/735386/…
  • 这是一个现有的代码。并且正在使用一个函数,该函数对于相同的条件执行会有不同的行为。
  • 然后想出一个简短的例子,你会使用它,这样我们就可以理解你的真正要求。就目前而言,如果我在编辑器中输入$var,我知道该变量的名称在perl 运行之前匹配什么模式。为什么我需要一个正则表达式匹配来计算它?

标签: regex perl


【解决方案1】:

每当您发现自己想要对变量名执行字符串比较时,请想“我应该使用哈希”。

另见How can I use a variable as a variable name in Perl?

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 2019-11-24
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2016-01-10
    • 2015-11-04
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多