考虑:
use 5.016;
use warnings "all";
use Data::Dumper;
my @abc = (1, 2, 3);
my @bbb = @abc[1..2];
my @ccc = @abc[1...2];
my @ddd = @abc[1....2];
say Dumper "@bbb"; # Output: '2 3'
say Dumper "@ccc"; # Output: '2 3'
say Dumper "@ddd"; # Output: ''
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码没有任何语法错误?
1...2这里的(三个点)和1....2(四个点)是什么意思?
我知道在Perl中,最常见的有效正则表达式是这样的:
$_ =~ m/regular expression/;
# and "m" can be omit
$_ =~ /regular expression/;
Run Code Online (Sandbox Code Playgroud)
我可以qr用来创建一个像这样的正则表达式引用:
my $regex = qr/regular expression/;
$_ =~ m/$regex/;
# and "m//" can be omit:
$_ =~ $regex;
Run Code Online (Sandbox Code Playgroud)
但我试过这个:
my $str = "regular expression";
$_ =~ $str; # why this is valid?
Run Code Online (Sandbox Code Playgroud)
它没有给我任何错误信息,并且工作正常.我不知道为什么,我认为应该是这样的:
my $str = "regular expression";
$_ =~ m/$str/;
# or
my $str = "regular expression";
my $regex = qr/$str/;
$_ =~ $regex;
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么$_ =~ $str在Perl中有效?
use 5.016;
use Data::Dumper;
my @var = (11, 22, 33);
# I think this is the TYPICAL use
say Dumper $var[1]; # output: 22
$var[1] = 88;
say Dumper $var[1]; # output: 88
# What's the difference?
say Dumper @var[1]; # output: 88
@var[1] = 99;
say Dumper @var[1]; # output: 99
Run Code Online (Sandbox Code Playgroud)
很长一段时间,我想如果我想访问数组中的一项,我只能使用$var[i](因为每本书都是这样写的)。
但是,最近我发现我@var[i]也可以使用它来访问该项目。
经过一番研究,我发现它似乎@var[i]是一种叫做切片的东西。
$var[1]和 和有什么区别@var[1]?如果我只用作@var[i]正确的值,是@var[i]和$var[i]一样吗?
在Python中,我们可以使用import readline,使raw_input()接受UP键,显示输入历史.
有没有办法在Ruby中做同样的事情?
我想在控制台中显示我在 Tk 窗口中按下的内容。
我写了以下代码:
require 'tk'
root = TkRoot.new
entry = TkEntry.new(root) do
pack
end
entry.bind("Key", proc {p "key pressed"})
Tk.mainloop
Run Code Online (Sandbox Code Playgroud)
key pressed如果我按任意键,它会显示。
但我想显示我按下的键,而不是一个常量字符串。谁能帮我?
我想用Python编辑~/.bash_profile,但是当我运行这些代码时:
f = open('~/.bash_profile', 'rb')
Run Code Online (Sandbox Code Playgroud)
它告诉我:
IOError: [Errno 2] No such file or directory: '~/.bash_profile'
Run Code Online (Sandbox Code Playgroud)
我以为是因为~/.bash_profile是一个系统保护文件.有没有方法可以使用Python打开这个文件?
我知道我们可以将任何代码#{}放在Ruby中,代码#{}将被计算然后插入到字符串中.
Perl中有没有相同的东西?
在 Python2 中,它是有效的:
#!/usr/bin/python
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
a = ListNode(0)
b = ListNode(1)
print(a < b)
Run Code Online (Sandbox Code Playgroud)
输出: True
但是在 Python3 中相同的代码,它会引发异常:
#!/usr/bin/python3
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
a = ListNode(0)
b = ListNode(1)
print(a < b)
Run Code Online (Sandbox Code Playgroud)
引发异常:
Traceback (most recent call last):
File "c.py", line 11, in <module>
print(a < b)
TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'
Run Code Online (Sandbox Code Playgroud)
为什么不一样?
添加:
我可以添加 …