Gossamer Forum
Home : General : Perl Programming :

Count elements in an array

Quote Reply
Count elements in an array
Hi,

I'm using an XML feed to supplement the links on my LSQL site, and have a small problem.

When the XML feed only returns a single result, my script fails offering the following error:

Not an ARRAY reference at /home/qango/public_html/cgi-local/dir/admin/Links/User/Search.pm line 197

I need it to either, display a single link from the feed without failing (something it hasn't been able to do in the past), or TRAP a single result and bypass the XML printing so the error doesn't occur.

Here's the relevant section of the Search.pm script, I wonder if anyone can help?

Code:


# Return if no results.
unless ($link_count or $cat_count) {
# CHECK FOR SPONSORED LINKS IF NO LOCAL RESULTS FOUND
# Get 20 XML results to put in the 'Sponsored Links - Not Found' search results template
my $XML_result = '';
my $query = $IN->param('query');
$query =~ s/ /\+/g;
$query =~ s/\W/+/g;
my $page = get("http://feed.feedpatrol.com/?affid=xxx&maxcount=20&search=$query&xtype=1&xformat=xml&ip=$ENV{HTTP_REFERER}");

# if we can't get the XML feed blank out the XML results and return
if (!$page) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# parse the XML feed to see if there are any results
my $ref = XMLin($page);
my $link_count = scalar($ref);
# if there are no results for this query from XML source, blank the XML results and return
if (!$link_count) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
else {
# Don't process the feed if there is only one XML link found (avoids ARRAY problem caused by single link only)
if ($link_count <= 1) {
return { error => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term };
}
# parse the XML feed into usable results for the template link_xml and link_xml_spon
my ($count, $html_links);
foreach ($count = 0; $count <= 20; $count++) {
# catch if there are no more results...
if (!$ref->{LISTING}->[$count]->{TITLE}) { last; }
# get the variables all set up...
my $Description = $ref->{LISTING}->[$count]->{DESCRIPTION};
my $Title = $ref->{LISTING}->[$count]->{TITLE};
my $Sitehost = $ref->{LISTING}->[$count]->{SITEHOST};
my $Sitelink = $ref->{LISTING}->[$count]->{LINK};
my $Sitelink = &url_decode ($Sitelink);
# put the links into HTML format...
$XML_result .= Links::SiteHTML::display('link_xml', { Description => $Description, Title => $Title, Sitehost => $Sitehost, Sitelink => $Sitelink });
}
}
}
return { xml => Links::language('SEARCH_NOLINKS'), search_banner => $banner_html, search_button => $button_html, search_sky => $sky_html, search_text => $text_html, term => $term, XML_result => $XML_result };
}


The line in red is where the error is reported from, and it appears that it fails because there isn't a second [#2] element in the array?

Any help with this much appreaciated.


All the best
Shaun
Quote Reply
Re: [qango] Count elements in an array In reply to
Hi,

This is a common problem with XML::Simple :(

Basically, if only one result is found... then its returned as a hash, while if more are found... its an array.

Something like this should help;

Code:
my @links;
if (ref $ref->{LISTING} eq 'ARRAY') {
for (my $i = 0; $i <= 50) {
my $link = $ref->{LISTING}[$i]; # may need to use $ref->{LISTING}->[$i]
if (!$link->{TITLE}) { last; }
push @links, $link;
}
} else {
my $link = $ref->{LISTING};
push @links, $link;
}

..then you can loop through the links;

Code:
foreach (@links) {
print qq{Title: $_->{TITLE}\n};
print qq{URL: $_->{URL}\n};
###etc
}

Hopefully that'll get you on the right tracks Smile

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Count elements in an array In reply to
Andy,

Thanks for replying.

I've now been given a snippet of code by the new search partner that seems to have solved the problem, so I didn't actually try yours - but if I get stuck in future I'll pop back and give your code a go Cool

Cheers
Shaun