in reply to Printing a Tag content

You could either use a giant regex or use split with <MS_.> as pattern.
The following code uses the split way.
Use brackets to catch the opening tags.
Stuff it in a %hash.
Grep out the elem you wanted.
Reassemble the record with map.
Print it and be happy.
#!/usr/bin/perl -w print ("Enter the word to search >> "); chomp ($word = <STDIN>); $/=undef; $_=<>; my @arr = split /(<MS_.>)/; shift @arr; # 1st element stuff before <MS_1>, so shift away my %hash = @arr; print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash;

Replies are listed 'Best First'.
Re^2: Printing a Tag content
by Anonymous Monk on Apr 03, 2006 at 20:57 UTC
    Thanks,

    Is this code of yours to go into the while loop of mine?? if note, the file with the tags is in a .txt file and I will need to open it and read the various lines. So, how do go about doing this.

    Edman

      No sorry. I was describing a way to read the xml from STDIN. If you want it using your $kwords file then use following version.
      open SEARCH, "<$kwords" or die "can't open $kwords $!\n"; { local $/=undef; $_ = <SEARCH>; my @arr = split /(<MS_.>)/; shift @arr; # 1st element stuff before <MS_1>, so shift away my %hash = @arr; print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash; } close SEARCH;
        Thanks you. And Thanks to all other monks for their assistance
Re^2: Printing a Tag content
by codeacrobat (Chaplain) on Apr 04, 2006 at 12:09 UTC
    Okies there is one more for the TIMTOWTDI and the golf fans.
    This is probably bad style but see yourself.
    #!/usr/bin/perl -w print ("Enter the word to search >> "); chomp ($word = <STDIN>); { local $/=undef; print grep { $_ =~ $word } <DATA> =~ m!(<MS_\d+>.*?</MS_\d+>)!gs; } __DATA__ <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>