0

我一直在研究一个代码,它将解析来自 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"; 

您可能获得的任何指导将不胜感激!谢谢!

4

2 回答 2

0

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

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

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

其他一些快速要点:

  • 写入文件然后打开它并在开始时再次读取内容没有多大意义。你已经在 $Parsed 中得到了你想要的东西。
  • 如果您只想单独打印变量,请不要将其放在引号中。这增加了很多开销。
于 2012-08-17T17:09:17.213 回答
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 实体正常,则可以省略 usingHTML::Entitiesdecode_entities($1)符号,否则可能会得到如下结果:

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

希望这可以帮助!

于 2012-08-17T17:50:17.980 回答