Two more ideas you could try...
1) Using a hash to store the data:2) Similar to the above but skipping the hash to make things a bit tighter:#!/usr/bin/perl use strict; my $data=(); open(IN,"xxxx.in"); while(<IN>){ /<(\w+)>(.*)<\/\1>/; $data{$1} = $2; } close(IN); print "Volume: $data{volume}\n"; print "Issue: $data{issue}\n"; print "Year: $data{year}\n";
#!/usr/bin/perl use strict; open(IN,"xxxx.in"); while(<IN>) { /<(\w+)>(.*)<\/\1>/; print ucfirst($1).": $2\n"; } close(IN);
The \1 is a back reference to whatever got matched in the first set of <>'s. You don't strictly need it as /<(\w+)>(.*)</ works just as well but it gives it all a nice sense of symmetry :D
The ucfirst is just there to make the tags neat but naturally you could just write the tags like that in the first place if you desired.
Both solutions work dynamically so you can change your tag names or add / remove tags without needing to modify your code. They should just as easily handle:
... which will produce:<volume>4</volume> <issue>12</issue> <year>2003</year> <pages>200</pages> <author>A N Other</author>
Volume: 4 Issue: 12 Year: 2003 Pages: 200 Author: A N Other
In reply to Re: problem with variables
by gothic_mallard
in thread problem with variables
by texuser74
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |