Rosetta Code/Find bare lang tags: Difference between revisions

From Rosetta Code
Content added Content deleted
m ({{omit from|GUISS}})
(finalize)
Line 1: Line 1:
{{draft task}}
{{task|Rosetta Code related}}
Find all <nowiki><lang></nowiki> tags without a language specified in the text of a page. Display counts by language section:
Find all <nowiki><lang></nowiki> tags without a language specified in the text of a page. Display counts by language section:


Line 24: Line 24:
1 in perl ([[Foo]])
1 in perl ([[Foo]])
2 in no language ([[Baz]])</nowiki></pre>
2 in no language ([[Baz]])</nowiki></pre>

For more extra credit, use the [http://rosettacode.org/mw/api.php Media Wiki API] to test actual RC tasks.


=={{header|Perl}}==
=={{header|Perl}}==
This is a simple implementation that does not attempt either extra credit.
<lang perl>my $lang = 'no language';
<lang perl>my $lang = 'no language';
my $total = 0;
my $total = 0;

Revision as of 16:04, 13 July 2011

Task
Rosetta Code/Find bare lang tags
You are encouraged to solve this task according to the task description, using any language you may know.

Find all <lang> tags without a language specified in the text of a page. Display counts by language section:

Description

<lang>Pseudocode</lang>

=={{header|C}}==
<lang C>printf("Hello world!\n");</lang>

=={{header|Perl}}==
<lang>print "Hello world!\n"</lang>

should display something like

2 bare language tags.

1 in perl
1 in no language

For extra credit, allow multiple files to be read. Summarize all results by language:

5 bare language tags.

2 in c ([[Foo]], [[Bar]])
1 in perl ([[Foo]])
2 in no language ([[Baz]])

For more extra credit, use the Media Wiki API to test actual RC tasks.

Perl

This is a simple implementation that does not attempt either extra credit. <lang perl>my $lang = 'no language'; my $total = 0; my %blanks = (); while (<>) {

 if (m/<lang>/) {
   if (exists $blanks{lc $lang}) {
     $blanks{lc $lang}++
   } else {
     $blanks{lc $lang} = 1
   }
   $total++
 } elsif (m/==\s*Template:\s*header\s*\\s*==/) {
   $lang = lc $1
 }

}

if ($total) { print "$total bare language tag" . ($total > 1 ? 's' : ) . ".\n\n"; while ( my ($k, $v) = each(%blanks) ) { print "$k in $v\n" } }</lang>