Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to print data between tags from file sequentially?

by haukex (Archbishop)
on Jun 22, 2018 at 08:09 UTC ( [id://1217168]=note: print w/replies, xml ) Need Help??


in reply to How to print data between tags from file sequentially?

I have a text file

Are you sure it's a text file? It looks like XML (or maybe HTML?) to me. Even if just a section of a longer text file contains XML data, I would strongly recommend extracting that section of text and using a proper module to parse it (see e.g. Parsing HTML/XML with Regular Expressions for why). Mojo::DOM has a nice interface, and it handles even this case of the XML not having a root node:

use warnings; use strict; use Mojo::DOM; my $html = <<'END_HTML'; <Answer type="string">ServerName</Answer> <Answer type="string">10.10.10.11</Answer> <Answer type="string">Windows Server 2012</Answer> END_HTML # note: ->xml(1) turns on case sensitivity my $dom = Mojo::DOM->new->xml(1)->parse($html); my $values = $dom->find('Answer')->map('all_text'); print "Computer: $values->[0]\n"; print "IP Address: $values->[1]\n"; print "OS: $values->[2]\n";

Minor edits

Replies are listed 'Best First'.
Re^2: How to print data between tags from file sequentially?
by marto (Cardinal) on Jun 22, 2018 at 09:17 UTC

    Good advice, use a proper parser, and I find Mojo::DOM makes things like this trivial, even for odd data.

Re^2: How to print data between tags from file sequentially?
by TonyNY (Beadle) on Jun 22, 2018 at 16:54 UTC
    Actually it is an xml file but redirected the output to a text file. Unfortunately the environment where I work is too strict for me to install any modules but thanks.
        I will check this out when I get a chance, thanks again

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217168]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-19 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found