I am trying to filter through a large XML file, pull out the elements I need within the value range restricted by the IF statement, and export them to CSV. However, I am a newb to programming and this is my first exposure to Perl, so I get syntax errors on lines 14 and 15 and probably doing other things wrong too. Please help me straighten this out!

Here's my code:

#! /usr/bin/perl -w use strict; use XML::Twig; my $twig= new XML::Twig( TwigHandlers => {town => \&town} ); if( my $file= $ARGV[0]) {$twig->parsefile( 'datafile_towns.xml'); $twig->flush;} sub town { my ($twig, $town) = @_; if (my $townx($town->first_child('location')->first_child('mapx'))>5 +00){ print $town->first_child('towndata')->first_child_text('townname') +, ',', $town->first_child('player')->first_child_text('allianc +eticker'), ',', $town->first_child('player')->first_child_text('playern +ame'), ',', $town->first_child('towndata')->first_child_text('townn +ame'), ',', $town->first_child('location')->first_child_text('mapy' +), ',', $town->first_child_text('location')->first_child_text(' +mapx'), print "\n"; }; $twig->flush; }

And here's a snippet of XML:

<towns> <server> <name>servername</name> <servername>servername</servername> </server> <town> <location> <mapx>383</mapx> <mapy>-1815</mapy> <terraintype id="9">terraintype</terraintype> <terrainoveralltype id="4">terraintype</terrainoveralltype> </location> <player> <playername id="1">somedude</playername> <playerrace>Human</playerrace> <playeralliance> <alliancename id="18">somealliance</alliancename> <allianceticker>AL</allianceticker> <alliancetaxrate>0.01</alliancetaxrate> </playeralliance> </player> <towndata> <townname id="1">sometown</townname> <foundeddatetime>date</foundeddatetime> <population>1</population> <iscapitalcity>1</iscapitalcity> <isalliancecapitalcity>1</isalliancecapitalcity> </towndata> </town>

In reply to TWIGging XML to CSV: what's wrong with my syntax? by tinker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.