Right now I have my code go through the XML file once, find all the comments, and put them in a temporary file using File::Temp. Here it is in pseudo code... <entry> <type>Post</type> <url>URL</url> <title>The title</title> <content>The blog post goes here</content> </entry> <entry> <type>Comment</type> <in-reference-to>URL</in-reference-to> <title>The title of the comment</title> <content>The content itself</content> </entry> ...
I then loop through the xml file again to find and print out all the actual blog posts. On each pass I search the entire temporary file for any comments that start with the post url and add those comments to the post, like so (again in pseudo code):my $tmpComments = File::Temp->new(SUFFIX=>'.txt') or die "File::Temp: +$!\n"; foreach (#Go through the whole xml file) { if ($type eq 'Comment') { # Parse the xml and save the parts as variables print $tmpComments "$posturl~~~$date~~~$author~~~$content\n"; # $posturl holds the <in-reference-to> url } }
foreach (#Loop through the xml file again) { print "$title\n"; print "$content\n"; ... seek $tmpComments, 0, 0 or die "Seek $tmpComments failed: $!\n"; #Re +wind temporary comments file while (my $line = <$tmpComments>) { # Split each line, store url as $commentID my @process_comment = split(/~~~/, $line); my $commentID = $process_comment[0]; # If the urls match, add it to the comments variable if ($commentID eq $posturl) { my $commentDate = $process_comment[1]; my $commentAuthor = $process_comment[2]; my $commentBody = $process_comment[3]; $comments.= "$commentDate | $commentAuthor | $commentBody"; } } print $comments; }
It works just fine, but it takes forever with longer xml files, probably since it's uselessly looping through the temporary file so many times.
Is there a more efficient way of doing this?
Is there a way to use a cache to store all the comments indexed by the URL so that when I loop through the blog posts I can look at the cache and pull out the appropriate comments?
In reply to Use temporary file or cache by andrewheiss
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |