in reply to Pasring XML into a simple hash
For a quick-and-dirty regexp solution, how about this...
NB I'm assuming the missing slashes in the data are a typo. If not then it's easy enough to modify the above.while ($text =~ m|<jobnumber>(.*?)</jobnumber>.*?<location>(.*?)</loc +ation>|sg) { print "Found job number $1 in location $2 \n"; }
Of course, a regexp solution isn't the right one if you want it to work in more general cases - like if the tags are the other way round, or whatever. If there's going to be any variation in the data, then the way to go is an XML parser as described by others above.
andy.
Some Time Later: Just For Fun, I tried to see if I could write one that /would/ work with the tags either way round... came up with this...
my $regexp = '<post>'; $regexp .= "(?=.*?<$_>(.*?)</$_>)" foreach qw(jobnumber location); $regexp .= '.*?</post>'; while ($stuff =~ m|$regexp|sog) { print "Found job number $1 in location $2 \n"; }
|
---|
In Section
Seekers of Perl Wisdom