in reply to Mojo::DOM find tag after another tag

Hi luxs,

What code have you tried?

Have a look at the "~" CSS selector in Mojo::DOM::CSS.

Update: The AM gave it away :-)

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Mojo::DOM find tag after another tag
by luxs (Beadle) on May 29, 2016 at 19:37 UTC
    The only script I've manage to do is
    my $tmpi = 0; $fimg = $dom ->find('img, h1') ->map( sub { if( $_->tag eq 'h1' ) { $tmpi++; } if( $tmpi > 0 && defined $_->attr->{'src'} ) { $_->attr->{'src'} } # without semicolumn!!!! } ); for( my $ii = 0; $ii <= $#$fimg; $ii++ ) { if( length( $fimg->[$ii] ) > 0 ) { print $fimg->[$ii]; } }
    looks too complicated and not nice (but working).

      Hi luxs,

      Since I'm guessing the <img> tags can be nested arbitrarily deep, the CSS selector "img, h1" was going to be my next suggestion.

      There are many other ways to write the logic, here's one:

      use warnings; use strict; use Mojo::DOM; my $data = <<'ENDDATA'; yada...yada...yada.. <img src="111"> yada...yada...yada.. <img src="222"> yada...yada...yada.. <h1>Some title</h1> yada...yada...yada.. <a href="444444"> <img src="333"> yada...yada...yada.. <img src="444"> ENDDATA my $dom = Mojo::DOM->new($data); my (@imgs,$seen_h1); for my $tag ( $dom->find('h1, img')->each ) { if ($seen_h1 && $tag->tag eq 'img' && length $tag->attr('src')) { push @imgs, $tag } elsif ($tag->tag eq 'h1') { $seen_h1 = 'true' } } print $_->attr('src'),"\n" for @imgs; __END__ 333 444

      You don't even need the intermediate @imgs array and can print directly from the first loop if you like.

      Hope this helps,
      -- Hauke D

        Yes, this way looks much more neat. Still struggle a bit to understand the logic of Mojo data and structure.