Hi Monks,

I am trying to collect a group of data from text file base on a couple of checks. Example: I have a text file that searched multiple xml file and extracted the following tags.

<MS_1> <loc>c:\data\cat.xml</loc> <words>dog, cat, fish, bird</words> </MS_1> <MS_2> <loc>c:\data\cow.xml</loc> <words>dog, cat, fish, bird, cow, goat</words> </MS_2> <MS_3> <loc>c:\data\snake.xml</loc> <words>dog, cat, fish, bird, snake, orange</words> </MS_3>

What I am trying to do is to prompt the user to enter the name of the animal to find. If the user enters snake for example, I want to return the entire content of <MS_3>..</MS_3> inculding the MS_ tags. I have tried the following code but I am getting nowhere. Could any monk kindly help me with this.

print ("Enter the word to search >> "); chomp ($word = <STDIN>); my $listen = 0; open(SEARCH, "<$kwords") while (<SEARCH>) { chomp; @array = (); foreach($_ =~ /\<MS_/){$listen++;} push (@array, $_) if /\<MS_$listen\>/../\<\/MS_$listen\>/; if ($_ =~ /$word/i) { print("@array[0..$#array]\n"); } }

Thanks,

Edman


In reply to Printing a Tag content by gzayzay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.