我今天在分割功能方面遇到了一些困难,并通过perlfunc读取 以查看我是否错误地解释了某些内容.我试图在'.'上分割一个字符串,因此应该支持perlfunc:
my $string = "hello.world";
my ($hello, $world) = split(".", $string);
Run Code Online (Sandbox Code Playgroud)
要么
my $string = "hello.world";
my ($hello, $world) = split(/\./, $string);
Run Code Online (Sandbox Code Playgroud)
但是,测试第一个导致空变量,所以我将测试扩展到以下内容:
#!/usr/bin/perl
use strict;
use warnings;
my $time_of_test = "13.11.19.11.45.07";
print "TOD: $time_of_test\n";
my ($year, $month, $day, $hr, $min, $sec) = split(/\./, $time_of_test);
print "Test 1 -- Year: $year month: $month day: $day hour: $hr min: $min sec: $sec\n";
($year, $month, $day, $hr, $min, $sec) = split(".", $time_of_test);
print "Test 2 -- Year: $year month: …Run Code Online (Sandbox Code Playgroud) 根据我的研究,perl print 语句中使用的 '\b' 字符应该像“退格”一样,即将光标向后移动一个字符,并删除当前字符。出于这个原因,我计划使用此操作在一行上打印操作状态,并随着进度进行更新。然而,我注意到,虽然光标确实向后移动,但脚下的字符并没有被删除,因此,在较短的打印语句之后仍保留较长的消息。我编译了以下示例代码来解释我的发现:
#!/usr/bin/perl
use strict;
use warnings;
my $m;
#set to nonzero so that the screen will update before \n
local $| = 1;
print "Current number shown: ";
$m = "LONG MESSAGE TEMP";
print $m;
print "\b" x length($m);
foreach(1..22) {
$m = $_;
print $m;
print "\b" x length($m);
#sleep 1; #Uncomment to see updates
}
print "\n";
Run Code Online (Sandbox Code Playgroud)
这是输出:
Current number shown: 22NG MESSAGE TEMP
如果这确实是 '\b' 的正确操作,是否还有另一种转义可以删除该字符并将光标向后移动?我想避免使用从当前行开头开始的“\r”。否则,我如何错误地使用转义符?