in reply to Re: I don't understand why I'm getting an "Use of uninitialized value" error
in thread I don't understand why I'm getting an "Use of uninitialized value" error

So how would you specify a certain tag if there is more than one? For example, I have two <Contact> tags in my xml. The only way I might be able to sort through it is to pick out the higher branch, say <Source> for one contact and <SourceB> for the other. So what I'm saying is return <Source><Countact>.

  • Comment on Re^2: I don't understand why I'm getting an "Use of uninitialized value" error

Replies are listed 'Best First'.
Re^3: I don't understand why I'm getting an "Use of uninitialized value" error
by GrandFather (Saint) on Nov 03, 2011 at 22:47 UTC

    Exactly that is demonstrated in the example for the section Processing just parts of an XML document. One way of achieving that is:

    #!/usr/bin/perl -w use warnings; use strict; use XML::Twig; my $xml = <<XML; <head><ID>This is an id</ID> <Book1> <Title> Title stuff </Title> </Book1> <Book2><Title>Another title</Title></Book2> </head> XML my $twig = XML::Twig->new( twig_roots => { ID => \&dump, TimeStamp => \&dump, IP_Address => \&dump, 'Book1/Title' => sub {title (1, @_);}, 'Book2/Title' => sub {title (2, @_);}, Complainant => \&dump, } ); $twig->parse($xml); sub dump { my ($t, $elt) = @_; (my $text = $elt->text()) =~ s/^\s+|\s+$//g; print "$text\n"; } sub title { my ($type, $t, $elt) = @_; (my $title = $elt->text()) =~ s/^\s+|\s+$//g; print "Title $type: $title\n"; }

    Prints:

    This is an id Title 1: Title stuff Title 2: Another title

    Take this code. Make sure you have XML::Twig installed. Play with it until you have some understanding of how it works.

    True laziness is hard work