为什么我会收到此错误?
use strict;
use warnings;
my $str = <<str;
88087 23/11/2010
35192 25/07/2010
B3J 5X9 17/08/2011
C8U 5L6 16/08/2011
F4Q 3B4 17/10/2010
D3X 9P4 11/05/2010
O7L 6Z8 28/02/2010
W8L 9P2 05/09/2010
str
print $str;
my @arr = split/\n/,$str;
foreach (@arr) {
my @tmp = split/\t/;
print "$tmp[1]\n";
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
php字符串转义为python的""""""?
python中的三引号包含其中包含的所有引号和换行符.例如,
""" this
is all
just one string. I can even tell you "I like pi".
Notice that the single quotes are escaped - they don't end the string; and the newlines become part of the string
"""
Run Code Online (Sandbox Code Playgroud)
有人知道PHP是否具有与python相同的功能
""" <form><h1>PUT HTML HERE</h1> </form> """
enter code here
Run Code Online (Sandbox Code Playgroud)
编辑:对于那些将来看这个问题的人,我已经回答了,这是一个例子:
$heading = "Heading Gettizburgz";
print <<< END
<p><h1>$heading</h1> "in quotes" 'in single'
Four score and seven years ago<br/>
our fathers set onto this continent<br/>
(and so on ...)<br/>
</p> …
有没有办法让Bash heredoc在heredoc中解释'\n \'?
我在循环中有一个迭代构建的字符串,类似于
for i in word1 word2 word3
do
TMP_VAR=$i
ret="$ret\n$TMP_VAR"
done
Run Code Online (Sandbox Code Playgroud)
然后我想在heredoc中使用创建的字符串:
cat <<EOF > myfile
HEADER
==
$ret
==
TRAILER
EOF
Run Code Online (Sandbox Code Playgroud)
但是我想将"\n"字符解释为换行符,以便输出为
HEADER
==
word1
word2
word3
==
TRAILER
Run Code Online (Sandbox Code Playgroud)
代替
HEADER
==
\nword1\nword2\nword3
==
TRAILER
Run Code Online (Sandbox Code Playgroud)
可能吗?或者我应该以某种方式构建我的初始字符串?
在红宝石heredoc:
a = <<~TEXT
asd
asd
TEXT
Run Code Online (Sandbox Code Playgroud)
它会产生:
[21] pry(main)> a = <<~TEXT
[21] pry(main)* asd
[21] pry(main)* asd
[21] pry(main)* TEXT
=> "asd\n" + "asd\n"
Run Code Online (Sandbox Code Playgroud)
它\n在字符串的末尾生成一个,如何避免这种情况?
给出以下代码:
$myString = <<<script
.
.
.
script;
Run Code Online (Sandbox Code Playgroud)
感谢这个问题的原始版本的答案,我理解<<<是heredoc语法,被视为双引号而不需要转义引号.
更进一步,这是如何最好地利用?具体来说,这是否应该减轻处理包含代码语法的混合引用字符串的压力?
I,E ..
attribute="name-like string" attribute="property: 'value("value")';"
Run Code Online (Sandbox Code Playgroud)
想到这可能是有用的(如果以我现在猜测的方式实现),特别是在处理更大的复杂性和/或寻找代码注入时.再次,寻找heredoc特别有用或可利用的任何场景.
我需要处理包含Java中的换行符的长字符串.这些是用于HTML生成的,但它并不是最重要的.
我知道Java在没有heredocs的方式上是残废的.但是我还可以使用其他机制:
1)字符串连接(或StringBuilders),不是非常易读和可复制粘贴.
2)在.properties文件中存储字符串,要么不是非常易读,要么具有更高的复制性.
3)将每个"heredoc"存储在单独的.txt文件中,非常易读且可以复制,但会产生大量的txt文件.
4)模板引擎,如Velocity或Freemarker - 将设计移出Java,需要大量的地图操作,这将是相当不错的,但Velocity语法和循环/如果能力不像Smarty那样可读
每个人都有利弊,我想选择3,但由于纯粹的意识形态原因,管理层更喜欢1.我想在Java中使用heredocs有一些标准,可能是库让事情变得更容易.我很感激任何建议(有很好的论据)如何使用heredocs.
感谢名单
想知道here-string(here-document)和pipe的正确用法是什么.
例如,
a='a,b,c,d'
echo $a | IFS=',' read -ra x
IFS=',' read -ra x <<< $a
Run Code Online (Sandbox Code Playgroud)
两种方法都有效.那么这两个功能有什么区别?
我对"阅读"的另一个问题是:
IFS=',' read x1 x2 x3 x4 <<< $a
Run Code Online (Sandbox Code Playgroud)
不起作用,x1的值为"abc d",而x2,x3,x4没有值
但如果:
IFS=',' read x1 x2 x3 x4 <<< "$a"
Run Code Online (Sandbox Code Playgroud)
我可以得到x1 = a,x2 = b,x3 = c,x4 = d一切都好!
有谁能解释一下?
提前致谢
在我的shell脚本中,我使用heredoc块来动态创建文件.什么是python等价物?
cat > myserver.pem << "heredoc"
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAnTsiYssvsuM1DRjyhqD8+ZB8ESqUFHgzeBYONp3yqjK8ICw/LRrxjXGXidAW
aPBXfktv3zN/kFsLMEFJKrJs/TLCfXG1CwFHMZzJRLM4aE6E0j6j+KF96cY5rfAo82rvP5kQdTIm
-----END RSA PRIVATE KEY-----
heredoc
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个简单的解决方案.我非常喜欢上面的shell脚本代码.我可以在python中"按原样"使用它吗?
我有一个应该在后台运行的脚本.我一打运行bash就必须回答一个问题.我怎么能这样做?
(nohup python script.py lst '<<HERE yes HERE' &)
Run Code Online (Sandbox Code Playgroud)