DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Perl / XML / Pattern Match

06.19.2007
| 5157 views |
  • submit to reddit
        This code parses through the games.xml file generated by EMUCenter and changes Genre to Letter, also taking the first letter of the Title property and adding it to the Letter property.  This allows sorting by letter in the EMUCenter interface. Two things, Change the metadata entry from Genre to Letter and remove the trailing </game> at the very end of the file.

my $holdTerminator = $/;
undef $/;

if (open(FILE, '<c:\games.xml')) {
	$file = <FILE>;
}
$/ = $holdTerminator;

@array = split /\<\/game\>/, $file;
open(FILE1, '>c:\newgames.xml');

foreach $line (@array){
	$line =~
s/(\n\s+.*?"Title">)(.)(.*?)"Genre">.*?(<\/property>)/$1$2$3"Letter">$2$4/is
;
	print FILE1 $line;
	print FILE1 '</game>';

}
close(FILE1);