2012-08-17 34 views
0

我一直在研究一個代碼,它將解析來自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"; 

你可能將不勝​​感激任何指導!謝謝!

+5

PerlMonks上的Crosspost:http://www.perlmonks.org/?node_id=988015(提及這一點很有禮貌,因此人們不會努力解決可能在其他地方解決的問題,協作努力可能基於全面討論,而不僅僅是部分討論。) – DavidO 2012-08-17 16:40:18

+1

請包括您正在使用的實際代碼(此代碼缺少一些'}')並且一致地縮進它。 – mob 2012-08-17 16:51:36

+0

你是否搜索了「Perl ical」?第一次打擊是關於如何解析iCal的文章,第二次和第三次是似乎處理您的問題的CPAN模塊。 – 2012-08-17 20:54:06

回答

0

我的靈感來自你的警告,我必須運行它。我甚至安裝了所需的模塊。你的電腦可能正在陷入無盡的循環,而不是真正的崩潰。

看着你的代碼,問題幾乎可以肯定你的索引。就目前而言,你的循環邏輯是一團糟。你最好的選擇是重新思考你是如何做到這一點的。而不是使用所有這些邏輯,請嘗試使循環依賴於通過文件。那樣的話,製造一個無限循環將會更加困難。另外,正則表達式將使這個工作更簡單。這可能不會做的正是你想要的,但它是一個開始:

while ($string =~ m/SUMMARY(.+?)DESCRIPTION(.+?)(?=SUMMARY|$)/gcs) 
{ 
    print "summary is: \n\n $1 \n\n description is: \n\n $2 \n\n"; 
} 

一些其他的快速點:

  • 寫入一個文件,然後打開它,並再次進行讀取內容回一開始沒有太大意義。你已經有了你想要的$ Parsed。
  • 如果你只是想自己打印一個變量,不要把它放在引號中。這增加了很多開銷。
0

也許下面將協助您解析任務:

use Modern::Perl; 
use LWP::Simple qw/get/; 
use HTML::Entities; 

my $html = get 'https://www.events.utoronto.ca/iCal.php?ical=1&campus=0&+sponsor%5B%5D=&audience%5B%5D=&category%5B%5D='; 

while ($html =~ /(Summary:\s*[^\n]+)\s*(Description:\s*[^\n]+)/gi) { 
    say decode_entities($1) . "\n" . decode_entities($2); 
} 

樣本輸出:

SUMMARY:Learning Disabilities Parent Support Group 
DESCRIPTION: Dates: Thursdays: May 24, June 21, and July 19 

SUMMARY:"Reading to Write" 
DESCRIPTION: Leora Freedman, Coordinator, English Language Learning Program, Faculty of Arts & Science 

SUMMARY:The Irish Home Rule Bill of 1912: A Centennial Symposium 
DESCRIPTION: One-day symposium presented by the Celtic Studies Program, St. Michael's College 

如果HTML實體的文本中都OK,你可以使用HTML::Entities和省略decode_entities($1)表示法,否則您可能會得到如下結果:

DESCRIPTION: Leora Freedman, Coordinator, English Language Learning Program, Faculty of Arts &amp; Science 

希望這有助於!