gzayzay has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing a Tag content
by GrandFather (Saint) on Apr 03, 2006 at 20:43 UTC | |
by codeacrobat (Chaplain) on Apr 03, 2006 at 20:49 UTC | |
|
Re: Printing a Tag content
by codeacrobat (Chaplain) on Apr 03, 2006 at 20:44 UTC | |
by Anonymous Monk on Apr 03, 2006 at 20:57 UTC | |
by codeacrobat (Chaplain) on Apr 03, 2006 at 21:23 UTC | |
by gzayzay (Sexton) on Apr 03, 2006 at 21:28 UTC | |
by codeacrobat (Chaplain) on Apr 04, 2006 at 12:09 UTC |