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.
c:\data\cat.xml dog, cat, fish, bird c:\data\cow.xml dog, cat, fish, bird, cow, goat c:\data\snake.xml dog, cat, fish, bird, snake, orange
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
print ("Enter the word to search >> ");
chomp ($word = );
my $listen = 0;
open(SEARCH, "<$kwords")
while ()
{
chomp;
@array = ();
foreach($_ =~ /\/../\<\/MS_$listen\>/;
if ($_ =~ /$word/i)
{
print("@array[0..$#array]\n");
}
}
Thanks,
Edman
Use [mod://XML::Twig] or [mod::XML::TreeBuilder]. Life is not long enough to roll your own XMH/HTML/CSV parser. To get you started:
use strict;
use warnings;
use XML::Twig;
use Data::Dump::Streamer;
my $twig = XML::Twig->new();
$twig->parse (do {local $/; });
my @chunks = $twig->root ()->children();
my %lookup;
for my $chunk (@chunks) {
my $words = $chunk->first_child ('words');
my $text = $words->xml_text ();
push @{$lookup{$_}}, $chunk for split /,\s+/, $text;
}
#do stuff with %lookup
__DATA__
c:\data\cat.xml
dog, cat, fish, bird
c:\data\cow.xml
dog, cat, fish, bird, cow, goat
c:\data\snake.xml
dog, cat, fish, bird, snake, orange
#!/usr/bin/perl -w
print ("Enter the word to search >> ");
chomp ($word = );
$/=undef;
$_=<>;
my @arr = split /()/;
shift @arr; # 1st element stuff before , so shift away
my %hash = @arr;
print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash;
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
open SEARCH, "<$kwords" or die "can't open $kwords $!\n";
{
local $/=undef;
$_ = ;
my @arr = split /()/;
shift @arr; # 1st element stuff before , so shift away
my %hash = @arr;
print map {($_, $hash{$_})} grep { $hash{$_} =~ $word } keys %hash;
}
close SEARCH;
#!/usr/bin/perl -w
print ("Enter the word to search >> ");
chomp ($word = );
{
local $/=undef;
print grep { $_ =~ $word } =~ m!(.*? )!gs;
}
__DATA__
c:\data\cat.xml
dog, cat, fish, bird
c:\data\cow.xml
dog, cat, fish, bird, cow, goat
c:\data\snake.xml
dog, cat, fish, bird, snake, orange
perlmonks.org content © perlmonks.org and Anonymous Monk, codeacrobat, GrandFather, gzayzay
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03