我正试图格式化。txt文件,以适合我的实验室正在使用的格式上传数据的项目。在.txt文件中是我用作博客条目id的url。但是,当我将它们作为STDIN传递给Perl脚本时,我得到了这个错误:
Illegal octal digit '8' at Desktop/blog.txt line 3, at end of line
这是它失败的URL:
http://thealbinobean.blogspot.com/2013/08/we-are-all-miley-cyrus.html
我知道发生错误是因为Perl将"08"解释为八进制数,但我无法找出Perl将整个URL解释为字符串的方法。下面是我的代码:
use strict;
use warnings;
my $count = 0;
my $blogName;
my $entryID;
# desired format:
# {{ID=URL}}{{USER=BLOG NAME}}
# Blog entry on one line
# "<ENDOFBLOG>"
foreach my $line (<STDIN>) {
if($count == 0) {
$blogName = $line;
} elsif($line =~ /^http/) {
$entryID = $line;
print "{{ID=$entryID}}{{USER=$blogName}}";
} elsif($line eq "<ENDOFBLOG>") {
print "<ENDOFBLOG>";
} elsif($line !~ /^s*$/) {
print $line;
}
$count++;
}
问题:如何让Perl将输入解释为字符串以避免这种八进制解释?
你运行程序了吗?我得到了大量的编译错误:
- 当你声明
$line
时,没有在foreach
循环中使用my
。 - 你的初始变量声明没有符号
- 您正在使用
$_
,但没有在任何地方设置它。 - 你没有在你的
print
语句中打印任何NL。 - 您正在使用
+
来连接您的字符串。这是加法。使用.
来连接
你甚至不需要这样做。Perl连接变量没有问题:
print "{{ID=$entryID}}{{USER=$blogName}}n";
让你的程序工作。修复这些错误。给我们更多的数据,这样我们就能弄清楚你的程序在做什么。
<标题>附录
我不明白为什么我没有得到这些编译错误。否则,我会在提交之前修复所有这些东西。也许它与我如何执行脚本并在Terminal中提供输入有关,因为我得到的唯一错误是上面列出的错误。
这是你原始计划:
use strict;
use warnings;
my count = 0;
my blogName;
my entryID;
# desired format:
# {{ID=URL}}{{USER=BLOG NAME}}
# Blog entry on one line
# "<ENDOFBLOG>"
foreach $line (<STDIN>) {
if(count == 0) {
blogName = $line;
} elsif($_ =~ /^http:/) {
entryID = $line;
print "{{ID=" + entryID + "}}{{USER=" + blogName + "}}";
} elsif($line eq "<ENDOFBLOG>") {
print "<ENDOFBLOG>";
} elsif($_ !~ /^s*$/) {
print $line;
}
count++;
}
下面是我得到的错误:
No such class count at ./test.pl line 5, near "my count"
syntax error at ./test.pl line 5, near "my count ="
No such class blogName at ./test.pl line 6, near "my blogName"
No such class entryID at ./test.pl line 7, near "my entryID"
Global symbol "$line" requires explicit package name at ./test.pl line 14.
Global symbol "$line" requires explicit package name at ./test.pl line 16.
Global symbol "$line" requires explicit package name at ./test.pl line 18.
Global symbol "$line" requires explicit package name at ./test.pl line 20.
Global symbol "$line" requires explicit package name at ./test.pl line 23.
Bareword "count" not allowed while "strict subs" in use at ./test.pl line 15.
Bareword "blogName" not allowed while "strict subs" in use at ./test.pl line 15.
Bareword "entryID" not allowed while "strict subs" in use at ./test.pl line 18.
Bareword "entryID" not allowed while "strict subs" in use at ./test.pl line 19.
Bareword "blogName" not allowed while "strict subs" in use at ./test.pl line 19.
Bareword "count" not allowed while "strict subs" in use at ./test.pl line 25.
Execution of ./test.pl aborted due to compilation errors.
标题>