tags:

views:

28

answers:

1

Hi,

I have html that looks like this,

<div>
   <span>Text 
       <a href="example.html">ABC</a>
       <a href="example.html">DEF</a>
       <a href="example.html">HIJ</a>
       <a href="example.html">KLM</a>
   </span>
   <p class="Time">
   09/28/10 - 03:46 PM EDT</p>
</div>

I need to loop through the <a> tags between the <span> tags till I get the text from all of them. This keeps changing from time so I do not know howmany <a> tags would exist. I cant do a,

while ( $tag = $stream->get_tag('a') ) 

because it browses through the <a> tags in the entire file. How can I make it stop when continuous <a> tags end?

+3  A: 
while ( $tag = $stream->get_tag(qw( a /span )) ) {
  last if $tag->[0] eq '/span';
  ...
}

Another approach would be to get any tag, and stop if it isn't <a>:

while ( $tag = $stream->get_tag ) {
  last unless $tag->[0] eq 'a';
  ...
}
cjm
It works great. Thanks!
Rajesh
Sometimes, simple $stream->get_tag is returning an error `Can't use string ("</h2>") as a HASH ref while "strict refs" in use` why?
Rajesh
@Rajesh, please post a new question showing the code that generates an error. That error message should indicate the line where the problem occurs. It's best if you can post a self-contained example script that exhibits the problem.
cjm