get_all_pages_in_category question

1 view
Skip to first unread message

natxo

unread,
Mar 11, 2011, 2:28:23 PM3/11/11
to Perlwikibot
hi,

When I use the get_all_pages_in_category method, it returns an array
as
expected. I would like to access them individually like so:

my @pages = $bot->get_all_pages_in_category("Category:XXXX");

while ( my $element = <@pages> ) {
print $element, "\n";
}

I would have expected to see one page per line, but what I get is one
line per word if the page title has spaces in it.

Is there a way to get the title in one line?

--
natxo

Mike.lifeguard

unread,
Mar 11, 2011, 4:00:49 PM3/11/11
to perlw...@googlegroups.com
Hello,

Although we spoke on IRC, I wanted to answer here as well, in case
others have a similar issue.

In Perl, you should use a foreach loop to iterate over an array:

my @pages = $bot->get_all_pages_in_category("Category:XXXX");

foreach my $page (@pages) {
print $page, "\n";
}

More concisely:
print "$_\n" for @pages; #or
say for @pages; # say means print with a newline, if you have 5.10+

Your example uses <> in scalar context, which means it is an implicit
iterator, using globbing. That makes it somewhat equivalent to:

while (my $elem = <*.c>) {
chmod 0644, $elem;
}

... which is totally not what you want. Operating on an array of
strings, you'll just end up doing glob expansion (which means splitting
on spaces).

The correct way to iterate over arrays in Perl is foreach.

I'm glad you find the library useful - let us know if you run into more
trouble. I'd be happy to help.

-Mike

Reply all
Reply to author
Forward
0 new messages