我一直在研究一個代碼,它將解析來自Ical源的事件信息。這是我想按關鍵術語劃分的一大塊數據。我需要它以有條不紊的方式完成。我嘗試索引關鍵術語,然後讓程序打印這些索引之間的內容。然而由於某種原因,它變成了無限循環打印所有的數據。我不知道如何解決它。不要運行我的代碼它會讓我的計算機凍結。我希望有人能告訴我我的問題是什麼。邏輯解析ICAL feed的難點
不要運行此程序
use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::FormatText;
my $URL= get("https://www.events.utoronto.ca/iCal.php?ical=1&campus=0&
+sponsor%5B%5D=&audience%5B%5D=&category%5B%5D=");
my $Format=HTML::FormatText->new;
my $TreeBuilder=HTML::TreeBuilder->new;
$TreeBuilder->parse($URL);
my $Parsed=$Format->format($TreeBuilder);
open(FILE, ">UOTSUMMER.txt");
print FILE "$Parsed";
close (FILE);
open (FILE, "UOTSUMMER.txt");
my @array=<FILE>;
my $string ="@array";
my $offset = 0; # Where are we in the string?
my $numResults = 0;
while (1) {
my $idxSummary = index($string, "SUMMARY", $offset);
my $result = "";
my $idxDescription = index ($string, "DESCRIPTION", $offset);
my $result2= "";
if ($idxSummary > -1) {
$offset = $idxSummary + length("SUMMARY");
my $idxDescription = index($string, "DESCRIPTION", $offset);
if ($idxDescription == -1) {
print "(Data malformed: missing DESCRIPTION line.)\n";
last;
}
if ($idxDescription > -1) {
$offset = $idxDescription+ length("DESCRIPTION");
my $idxLocation= index($string, "LOCATION", $offset);
if ($idxLocation == -1) {
print "(Data malformed: missing LOCATION line.)\n";
last;
}
my $length = $idxDescription - $offset;
my $length2= $idxLocation - $offset;
$result = substr($string, $offset, $length);
$result2= substr ($string, $offset, $length2);
$offset = $idxDescription + length("DESCRIPTION");
$result =~ s/^\s+|\s+$//g ; # Strip leading and trailing white space, including newlines.
$result2 =~ s/^\s+|\s+$//g ;
$numResults++;
} else {
print "(All done. $numResults result(s) found.)\n";
last;
}
open (FILE2, "UOT123.txt")
print FILE2 "TITLE: <$result>\n DESCRIPTION: <$result2>\n";
你可能將不勝感激任何指導!謝謝!
PerlMonks上的Crosspost:http://www.perlmonks.org/?node_id=988015(提及這一點很有禮貌,因此人們不會努力解決可能在其他地方解決的問題,協作努力可能基於全面討論,而不僅僅是部分討論。) – DavidO 2012-08-17 16:40:18
請包括您正在使用的實際代碼(此代碼缺少一些'}')並且一致地縮進它。 – mob 2012-08-17 16:51:36
你是否搜索了「Perl ical」?第一次打擊是關於如何解析iCal的文章,第二次和第三次是似乎處理您的問題的CPAN模塊。 – 2012-08-17 20:54:06