#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example shows how to use the purge method                       #
#  It outputs the name of the leader in a statistical category          #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $leader_name;
my $leader_score=0;


my $field= $ARGV[0] || 'ppg';
                                              # create the twig
my $twig= new XML::Twig( twig_handlers => { player => \&player } ); 

$twig->parsefile( "nba.xml");                 # parse the twig
                                              # print the result
print "Leader in $field: $leader_name ($leader_score $field)\n";

sub player
  { my( $twig, $player)= @_;                      
                                              # get the score
    my $score= $player->first_child( $field)->text;    
    if( $score > $leader_score)               # if it's the highest
      { $leader_score= $score;                # store the information
        $leader_name= $player->first_child( 'name')->text;
      }
    $twig->purge;                             # delete the twig so far   
 }

