gzayzay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Few days a go, I asked about reading the content of a xml tag without the use of XML::Parser, and other modules. Today while fumbling around I wrote a code on how to read the content of specific tags and I thought I should share it.

open(DBASE, $path."database.txt") || die ("File does not exist, $!\n") +; while(<DBASE>) { chomp; print (LIST "\nTIME: ".localtime(time)."\n"); print (LIST "$_\n\n"); open (XFILE, $_) || die ("Can\'t open[$_], $!\n"); while(<XFILE>) { chomp; if($_ =~ /\<name\>/../\<\/name\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<keywords\>/../\<\/keywords\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<description\>/../\<\/description\>/) { print (LIST "$_\n"); } elsif($_ =~ /\<results\>/../\<\/results\>/) { print (LIST "$_\n"); } }close XFILE; } close LIST; close DBASE;

The above code is working find but if any monks could help me make it smarter, I will really appreciate it.

Thanks,

Edman

Replies are listed 'Best First'.
Re: Reading b/w tags
by mickeyn (Priest) on Mar 21, 2006 at 07:46 UTC
    hi gzayzay,

    try to replace your 'while' loop with this one:

    while (<XFILE>) { chomp; print (LIST "$_","\n") if /\<(name|keywords|description|results)\>/ .. /\<\/$1\>/; }

    Enjoy,
    Mickey

      Thanks a lot Mickey.

      Edman