在Perl中,我正在使用XML::Twig读取XML文件.某些属性的文本如下所示:
<p>Here is some text.</p>

<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
我正在将此属性读入名为的变量中$Body.我想将这个变量打印到文件而不插入字符串中的特殊字符,即输出应该看起来与输入完全一样.我的代码看起来像:
open (my $OUT, ">", "out.csv") or die $!;
print $OUT $Body;
Run Code Online (Sandbox Code Playgroud)
但是,当我查看时out.csv,我看到:
<p>Here is some text.</p>
<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
相反,我想看看原始字符串:
<p>Here is some text.</p>
&;#xA;<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
我试过以下但没有成功:
print $OUT '$Body'; 不起作用,只显示"$ Body"print $OUT "$Body"; 不起作用,与没有引号相同.print $OUT qw{$Body}; 不起作用,只显示"$ Body".
这是一个完整的例子:
tmp.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<node Body="<p>Here is some text.</p>

<p>Some more text."/>
</root>
Run Code Online (Sandbox Code Playgroud)
码:
#!/usr/bin/perl
use strict;
use XML::Twig;
my $t=XML::Twig->new();
$t->parsefile("tmp.xml");
my $root= …Run Code Online (Sandbox Code Playgroud) 对于给定Map的常数
val ctt = Map("a" -> 1, "b" -> 2)
Run Code Online (Sandbox Code Playgroud)
如何定义String插值器c在哪里
c"a"
Run Code Online (Sandbox Code Playgroud)
交付List(1)?
注意已经考虑过字符串插值但仍不清楚如何继续.
更新
c"a,b"
res: List(1,2)
c" a, b "
res: List(1,2)
c"a,w"
res: List(1)
c"w"
res: List()
Run Code Online (Sandbox Code Playgroud) 是否可以按名称获取变量?
我尝试构建以下函数,但它没有按预期工作......
@function variable-lookup($variable, $suffix: "") {
$value: null;
@if ($suffix != "" and global-variable-exists($variable+"-"+$suffix)) {
$value: #{$variable+"-"+$suffix};
}
@else if (global-variable-exists($variable)) {
$value: #{$variable};
}
@return $value;
}
Run Code Online (Sandbox Code Playgroud)
这是一个如何使用它的示例:
$primary: #000;
$primary-hover: blue;
a {
color: variable-lookup("primary", "base");
&:hover {
color: variable-lookup("primary", "hover");
}
}
Run Code Online (Sandbox Code Playgroud)
当我想围绕这个“变量查找”函数编写一堆特定于上下文的速记包装函数时,真正的力量就会出现。
任何想法如何实现这一目标?
不幸的是,最直观的方式,
val world = "Earth"
val tree = q"""println("Hello $world")"""
Run Code Online (Sandbox Code Playgroud)
结果是
Error:(16, 36) Don't know how to unquote here
val tree = q"""println("Hello $world")"""
^
Run Code Online (Sandbox Code Playgroud)
因为$在quasiquotes中期望a tree.
val world = "Earth"
val tree = q"""println(${c.literal(s"Hello $world")})"""
Run Code Online (Sandbox Code Playgroud)
工作,但非常难看我得到一个Intellij警告,c.literal不赞成,我应该使用quasiquotes,而不是.
那么......我该怎么做?
UPDATE
回应flavian的评论:
import scala.language.experimental.macros
import scala.reflect.macros._
object TestMacros {
def doTest() = macro impl
def impl(c: blackbox.Context)(): c.Expr[Unit] = {
import c.universe._ //access to AST classes
/*
val world = "Earth"
val tree = q"""println(${c.literal(s"Hello $world")})"""
*/ …Run Code Online (Sandbox Code Playgroud) 在Angular2中,为什么我将一个属性放在一个简单的视图中,如下所示:
<span>{{allowServer}}</span>
Run Code Online (Sandbox Code Playgroud)
每当它的.ts文件的值发生变化时它就会改变,如果我把它改为:
<button class="btn btn-primary" disabled={{allowServer}} >server</button>
Run Code Online (Sandbox Code Playgroud)
该按钮没有禁用新值?
那么,我必须使用插值而不是绑定语法的规则是什么?
[disabled]=allowServer
Run Code Online (Sandbox Code Playgroud) 是否有更简单的方法在下面的插入 SQL 命令中使用name和phone变量?
字符串插值是一种方法,但我不知道如何实现。
String name = textBox1.Text;
String phone = textBox2.Text;
var query = "insert into Customer_info(Customer_Name,Customer_Phone) " +
"values('" + name + "','" + phone + "');";
SqlCommand com = new SqlCommand(query,con);
try {
con.Open();
com.ExecuteNonQuery();
con.Close();
}
catch (Exception Ex) {
con.Close();
}
Run Code Online (Sandbox Code Playgroud) 如何将字符串"foo, bar, xid, uid"转换为列表["foo", "bar", "xid", "uid"]?
我正在分配来自 Consul 的变量值,它是一个字符串
my_var = "foo, bar, xid, uid"
Run Code Online (Sandbox Code Playgroud)
现在我想转换my_var为一个如下所示的列表:
my_list = ["foo", "bar", "xid", uid"]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我试图使用formatlistsplat 操作符,但没有成功
my_list = ${formatlist(<put magic here>, var.my_var)
Run Code Online (Sandbox Code Playgroud) 从本质上讲,我有一个用于插值的字符串:Log_{0}.txt并且{0}在不同的过程中将其替换为整数。因此结果可能类似于Log_123.txt或Log_53623432.txt。
我正在尝试使用检查数字的正则表达式string.Format()替换{0}。最终,我也希望能够提取这些数字。
我已经尝试过一些类似的变体,但是我没有任何运气:
var stringFormat = new Regex(string.Format("Log_{0}.txt", @"^\d$"));
Run Code Online (Sandbox Code Playgroud)
另外,这是检查格式的代码:
var fileNameFormat = new Regex(string.Format("Log_{0}.txt", @"^\d+$"));
var existingFiles = Directory.GetFiles("c:\\projects\\something");
foreach(var file in existingFiles)
{
var fileName = Path.GetFileName(file);
if(fileNameFormat.Match(fileName).Success)
{
// do something here
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用这样的代码:
public static class Program {
public static void Main() {
Console.WriteLine(hello);
}
internal static readonly string hello = $"hola {name} {num}";
internal static readonly string name = $"Juan {num}";
public const int num = 4;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,当我获得 hello 的值时,它返回给我“hola 4”,因此在插入另一个使用插值的字符串时似乎存在问题。我的预期行为是“hola Juan 4 4”,或者如果语言不支持这种链式插值,则编译时会出错。
有人知道为什么 C# 会出现这种行为吗?