#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example shows how to create, and paste elements                 #
#  It creates a new element named blg, for each player                  #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $twig= new XML::Twig;

$twig->parsefile( "nba.xml");    # build the twig
my $root= $twig->root;           # get the root of the twig (stats)
my @players= $root->children;    # get the player list

                                 
foreach my $player (@players)     
 { my $g  = $player->first_child( 'g')->text;    # get the text of g            
   my $blk= $player->first_child( 'blk')->text;  # get the text of blk
   my $blg= sprintf( "%2.3f", $blk/$g);          # compute blg
   my $eblg= new XML::Twig::Elt( 'blg', $blg);   # create the element
   $eblg->paste( 'last_child', $player);         # paste it in the document   
 }

$twig->print;                    # note that we lose the extra returns
