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

This looks like you are parsing XML. You should really consider using a module like XML::Twig to do the heavy lifting for you:

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

Prints:

This is an id Title stuff Another title
True laziness is hard work

Replies are listed 'Best First'.
Re^2: I don't understand why I'm getting an "Use of uninitialized value" error
by TheBigAmbulance (Acolyte) on Nov 03, 2011 at 21:13 UTC

    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>.

      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
Re^2: I don't understand why I'm getting an "Use of uninitialized value" error
by TheBigAmbulance (Acolyte) on Nov 03, 2011 at 20:29 UTC

    It involves a XML. I have a source XML. What I'm trying to accomplish is open the XML once, and have some form of perl 'scripting thingy' run through the xml multiple times to pick out the tags.

    I.E. If there is a tag in the xml like '<ID>test</ID>', set the variable $ID to 'test'. If there is a timestamp '<TimeStamp>2011-09-24T21:38:11Z</TimeStamp>', pull out '2011-09-24T21:38:11Z'. I'm a novice when it comes to perl/xml interactions. Just trying to get the information out.

      Yes, but did you take note of what I said. Parsing XML is hard. Especially if you are new to this stuff: don't reinvent the wheel (see Re: Reinventing the wheel and Re: Reinventing the wheel). Try running the code I supplied. Try running it against a sample of your real data. Skim read the XML::Twig documentation. I know it looks really intimidating, but it contains really good examples and will save you much more time debugging your own hand written XML parsing code than you spend figuring out how to use the module.

      On a more general programming style note: "run through the ... multiple times" is almost always a red flag in programming (and most other places too). The more times you have to perform a slow operation the slower the overall job will be. Turn your inner two loops inside out so you read the file once and perform multiple matches per line. Compared with accessing information in memory accessing data from disk is very slow.

      True laziness is hard work