【问题标题】:Difficulty with Logic parsing ICAL feed逻辑解析 ICAL 提要的困难
【发布时间】:2012-08-14 02:56:36
【问题描述】:

我一直在编写一个代码,该代码将解析来自 Ical 提要的事件信息。这是一个巨大的数据块,我想按关键词进行划分。我需要有条不紊地进行。我尝试为关键术语编制索引,然后让程序打印这些索引之间的内容。但是由于某种原因,它变成了打印所有数据的无限循环。我不知道如何解决它。不要运行我的代码,它会一直冻结我的计算机。我希望有人能告诉我我的问题是什么。

不要运行这个程序

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 上的交叉发帖:perlmonks.org/?node_id=988015(礼貌地提一下,这样人们就不会努力解决可能已经在别处解决的问题,并且协作努力可能基于整个讨论,而不仅仅是其中的一部分。)
  • 请包含您正在使用的实际代码(此代码缺少一些}'s)并一致缩进。
  • 您在 Google 上搜索过“Perl ical”吗?第一个热门文章是关于如何解析 iCal 的文章,第二个和第三个是似乎可以解决您的问题的 CPAN 模块。

标签: perl parsing logic icalendar


【解决方案1】:

您的警告让我深受启发,不得不运行它。我什至安装了这样做所需的模块。您的计算机可能只是陷入了无限循环,而不是真正崩溃。

查看您的代码,问题几乎可以肯定是您的索引。就目前而言,您的循环逻辑有点混乱。你最好的选择是重新考虑你是如何做到这一点的。不要使用所有这些逻辑,而是尝试使循环依赖于遍历文件。这样一来,就很难形成无限循环。此外,正则表达式将使这项工作变得更加简单。这可能并不完全符合您的要求,但这是一个开始:

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

其他一些要点:

  • 写入文件然后打开它并在开始时再次读取内容没有多大意义。您已经在 $Parsed 中获得了您想要的内容。
  • 如果您只想单独打印一个变量,请不要将其放在引号中。这会增加很多开销。

【讨论】:

    【解决方案2】:

    也许以下内容可以帮助您完成解析任务:

    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 实体正常,您可以使用 HTML::Entitiesdecode_entities($1) 表示法省略,否则您可能会得到如下结果:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2021-10-08
      相关资源
      最近更新 更多