stuie has asked for the wisdom of the Perl Monks concerning the following question:

Hi, my mastery of Perl is much like Tarzan's mastery of English, so please bear with me if I seem a bit ignorant.

I'm using the rss2html.pl script available with the XML::RSS module to print out RSS news items out of our site into HTML. The problem is that we want to be able to sort by category and by date. The issue is that the data is stored in a bunch of multi-dimensional hashes, and I'm trying to work out the best strategy to output the information.

The cast of characters includes:

The best strategy I've figured out is to push each item's hash values into an array, push this array into a parent array, and then sort the parent array by the nzgls:identifier info. However, when I try doing this I get errors about turning hashes into arrays. I get the feeling I'm trying to do something that Perl isn't designed to do.

Any ideas? Thanks for reading this far!

Cheers

Stuart

Replies are listed 'Best First'.
Re: Sorting RSS items out of XML::RSS
by Fletch (Bishop) on Aug 17, 2004 at 02:24 UTC

    If you're having problem extracting what you want in place from the $rss instance, the best first step might be to extract all the information you're interested in (description, title, your kiwi tags :) from it and into a new data structure. Then operate on that rather than trying to manipulate the raw data (not that you probably couldn't do it in place, it may be easier to think about it in your terms rather than XML::RSS').

      Thanks Fletch!

      I have tried to create my own structure, along the following lines:

      foreach my $item (@{$rss->{'items'}}) { my $title=$item->{'title'}; my $link=$item->{'link'}; my $description=$item->{'description'}; my $date_valid=$item->{nzgls}->{'date.valid'}; my $doc_type=$item->{nzgls}->{'type.document'}; my $identifier=$item->{nzgls}->{'identifier'}; @holdarray="$title", "$link", "$date_valid", "$doc_type", "$identifier", "$description"; }

      So far, so good. But when I get down to building a parent array to store the items in for sorting...

      push @megarray, @holdarray; print "$megarray[0][1]\n";

      I get: "Can't use string ("") as an ARRAY ref while "strict refs" in use at ./rss-experiment.pl line 114."

      I'm thinking the line when I push @holdarray into @megarray probably isn't doing what I'm thinking it is :)

      Thanks,

      Stuart

        You need to use array references rather than arrays, so replace your:
        push @megarray, @holdarray;
        by
        push @megarray, \@holdarray;
Re: Sorting RSS items out of XML::RSS
by Zaxo (Archbishop) on Aug 17, 2004 at 05:53 UTC

    You can get a lightweight array of the items by just copying the values of %$rss. That will give you a new array whose elements still refer to the original data. The new array can be sorted as you might the original hash, but being an array you can make the sort stick. All you lose are the keys, but perhaps there is other identification in the data.

    my @items = values %$rss; my @sorted_items = sort { $a->{'nzgls'}{'unixtime'} <=> $b->{'nzgls'}{'unixtime'} } @items; # or just # my @sorted_items = sort { # $a->{'nzgls'}{'unixtime'} <=> $b->{'nzgls'}{'unixtime'} # } values %$rss;
    I've assumed unix times because thay are one of the simplest to sort. Other formats will need other comparisons.

    After Compline,
    Zaxo