ChocolateCake has asked for the wisdom of the Perl Monks concerning the following question:
I have been working on a code that will parse event information from an Ical feed. It is a huge block of data that I want to divide by key term. I need it to be done in an orderly way. I tried indexing the key terms and then having the program print what is between those indexes. However for some reason it became in infinite loop that printed all the data. I don't know how to fix it. DO NOT RUN MY CODE IT KEEPS FREEZING MY COMPUTER. I was hoping someone could show me what my problem is.
DO NOT RUN THIS PROGRAM 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, $result2 =~ s/^\s+|\s+$//g ; # includng newlines. $numResults++; } else { print "(All done. $numResults result(s) found.)\n"; last; } open (FILE2, "UOT123.txt") print FILE2 "TITLE: <$result>\n DESCRIPTION: <$result2>\n";
Any guidance you may have will be greatly appreciated! Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Difficulty with Logic parsing ICAL feed
by roboticus (Chancellor) on Aug 17, 2012 at 16:30 UTC | |
by ChocolateCake (Initiate) on Aug 17, 2012 at 17:02 UTC | |
by roboticus (Chancellor) on Aug 17, 2012 at 18:50 UTC | |
Re: Difficulty with Logic parsing ICAL feed
by davido (Cardinal) on Aug 17, 2012 at 16:42 UTC | |
by ChocolateCake (Initiate) on Aug 17, 2012 at 17:03 UTC | |
by MidLifeXis (Monsignor) on Aug 17, 2012 at 17:16 UTC | |
Re: Difficulty with Logic parsing ICAL feed
by tobyink (Canon) on Aug 17, 2012 at 16:28 UTC | |
Re: Difficulty with Logic parsing ICAL feed
by davido (Cardinal) on Aug 17, 2012 at 16:55 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom