On Fri, 15 Nov 2002, Sorin Marti wrote:

> Hi all,
> 
> I,ve got a problem. Following Code reads an Example-String in XML-Style, 
> filters out the necessary information and writes it in a HASH. Now I 
> want to read a file and not only one String. I thought that I can just 
> put a while-loop around my code but that don't work. How may I solve that.

For the xml part of your work I would suggest you use one of the xml::*
modules from cpan. Go to http://search.cpan.org , search and take your 
pick.

> 
> 
> #######################
> #!/usr/bin/perl
#!/usr/bin/perl -w
use strict;

# This will save a lot on your debugging time

> $text = '<energietraeger>Erdoel</energietraeger> 
> <umwandlung>Verbrennung</umwandlung>';
> $tmp = $text;
> while($tmp =~ /<[^\>]+\>/){
>   $tmp =~ s/<([^\>]+)\>([^<]+)<\/[^\>]+\>//;
>   $werte{$1} = $2;

Use $1, $2 ... only within a condition check, like this
if ($tmp =~ s/..../) {
  $werte{$1} = $2;
}
These variables are not cleared, if the current match fails
they will still contain values from the previous capture.

> }
> foreach $key(keys(%werte))
>   {
>     print $key." - ".$werte{$key}."\n";
>   }
> #######################
> 
> what i get out is:
> 
> energietraeger - Erdoel               
> umwandlung - Verbrennung      
> 
> what I tried:
> 
> open (FILE, "<test.xml");
> while($text = <FILE>)
> { ##CODE ABOVE## }
> 
> then nothing happends

What is the date in test.xml, are you sure your regexp
passed on the contents of test.xml?

> 
> thanks for every advice
> Sorin


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to