
mhx
Enthusiast
/ Moderator
May 20, 2002, 12:38 AM
Post #6 of 7
(584 views)
|
|
Re: [alalleyn] Repeatly reading information between two tags
[In reply to]
|
Can't Post
|
|
The problem with your script is the following part:
while( <TXT> ) { @code = ($text) = do { local $/=undef; <TXT> } =~ /<programlisting>(.*?)<\/programlisting>/gsi; } First of all, the while loop is wrong, since the loop's body slurps the whole text file at once. The tricky thing is that the while( <TXT> ) will read the first line of the text file which will never be used. So, let's remove the while loop:
@code = ($text) = do { local $/=undef; <TXT> } =~ /<programlisting>(.*?)<\/programlisting>/gsi; The do { local $/=undef; <TXT> } will read the whole text file. You don't need to assign undef since localizing a variable will automatically undefine it locally. However, that part - including the regex - is absolutely correct. The problem is here:
@code = ($text) = do { local $/; <TXT> } =~ /<programlisting>(.*?)<\/programlisting>/gsi; You are first assinging the list of all matches to ($text), which will keep the first match in $text and throw away all other matches. Next, that single match is assigned to @code. If you had left out the assignment to ($text) (which you don't need anyway):
@code = do { local $/; <TXT> } =~ /<programlisting>(.*?)<\/programlisting>/gsi; or at least had written it the other way round:
($text) = @code = do { local $/; <TXT> } =~ /<programlisting>(.*?)<\/programlisting>/gsi; the solution would have worked. All other changes I've made are more or less for style. I'm a use strict; fanatic, and I don't like embedded HTML code very much... Hope this helps. -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|