in reply to Premature End-of-File - Scope problems?

Your data looks rather like it may be XML, in which case you may be better to look at some of the XML modules such as XML::Twig and XML::TreeBuilder. As an example of how these things can help consider:

#! /bin/perl -w use strict; use warnings; use XML::TreeBuilder; my $root = XML::TreeBuilder->new (); $root->parse (do {local $/; <DATA>;}); my $frame_marker = 'data'; my @dataNodes = $root->look_down ('_tag', 'data'); my $nodeCount; for my $nodeIndex (0 .. @dataNodes - 1) { my @strings = $dataNodes[$nodeIndex]->look_down ('_tag', 'string') +; print "Data node " . ($nodeIndex + 1) . "\n"; print " ", $_->as_text (), "\n" for @strings; } __DATA__ <root> <data> <string>1 some stuff</string> <string>1 some more stuff</string> <string>1 yet more stuff</string> <string>1 enough stuff</string> </data> <data> <string>2 some stuff</string> <string>2 some more stuff</string> <string>2 yet more stuff</string> <string>2 enough stuff</string> </data> <data> <string>3 some stuff</string> <string>3 some more stuff</string> <string>3 yet more stuff</string> <string>3 enough stuff</string> </data> </root>

Prints:

Data node 1 1 some stuff 1 some more stuff 1 yet more stuff 1 enough stuff Data node 2 2 some stuff 2 some more stuff 2 yet more stuff 2 enough stuff Data node 3 3 some stuff 3 some more stuff 3 yet more stuff 3 enough stuff

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Premature End-of-File - Scope problems?
by bratwiz (Sexton) on Feb 15, 2007 at 00:40 UTC
    Yes, it is XML. I do have that squared away. For my purposes I don't need a tree, just to gracefully flatten it out for use in a monitor. Wanted it to be XML so I'd be positioned to go to a more standards-based solution in the future (I'm programming at the point of a gun lately-- my boss wants it now now now-- actually that's not true, he wants it _yesterday_ :) So quick-n-dirty is the order today.

      "Quick and dirty" often turns to "slow and messy". For example, compare the two "test" scripts in this thread. ;)

      Using XML::TreeBuilder was "Quick and dirty". A XML::Twig solution is likely to be more appropriate for a scalable solution. Consider:

      #! /bin/perl -w use strict; use warnings; use XML::Twig; my $dataCount = 0; my $str = do {local $/; <DATA>}; my $t= XML::Twig->new (twig_roots => {data => \&data}); $t->parse ($str); sub data { my ($t, $data) = @_; ++$dataCount; print "Data node $dataCount\n"; my @strings = $data->descendants ('string'); print " ", $_->trimmed_text (), "\n" for @strings; } __DATA__

      using the same data as the previous sample orints:

      Data node 1 1 some stuff 1 some more stuff 1 yet more stuff 1 enough stuff Data node 2 2 some stuff 2 some more stuff 2 yet more stuff 2 enough stuff Data node 3 3 some stuff 3 some more stuff 3 yet more stuff 3 enough stuff

      Note that both samples cheat by wrapping a root element around the data elements to form a more compliant XML document.


      DWIM is Perl's answer to Gödel
        Absolutely no debate there, quick-n-dirty is generally not a good way to go. In this case its a project I outlined about a year-and-a-half ago and then had to shelf due to other assignments. Now my boss is telling me he wants it up and running yesterday (nevermind he hasn't actually given me any time to do it). MY own estimation to do the project "right" is a couple of months. He wanted it in three days (by "it" he means something useful he can give to another group that does monitoring, who has nothing at all now). The "real" answer, when I am permitted more time to work on it properly, is to define a well-balanced set of objects, bind them to xml-ish config files and thus make something beautiful and easily reconfigured. It really grates on me doing all this "hard-coding". I hate it. I already have some of the important parts built though, and they're already reasonably configurable. And I'm working from a pretty well fleshed-out concept document that I did awhile back, so I have a pretty-good roadmap to follow. I just wish my boss understood that writing programs takes time. You can't just assign me a program on Monday, tell me its due on Friday, and then suck up all my time on other, unrelated projects all week and then expect it to magically pop out on the other end... (sigh) What can ya do?? :)