Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Roger, you are correct about the structure of the code, and that the print statement has to be in the proper place in the loop. I am now saving 99% of what I need into a new file.

I have quite a few loops as I sweep through the HTML to get my file name, and now everything I want gets printed into my file except the bit of text that I pull out to use as a file name for the image. The relevant code is this:

{ push @next, $p->get_token(); if ($next[1][0] eq 'T') { my $title= $next[1][1]; #strip white space from title and get up to four words if ($title =~ /(\w+)\s*(\w+)\s*(\w+)\s*(\w+)/) { my $filetitle = $1 . $2 . $3 . $4; my $newsrc = $filetitle . "\.jpg"; while ( $token=$p->get_token() ) { if ($token->is_start_tag('img') ) { my $src = $token->return_attr->{src}; $token->set_attr('src', $newsrc); } print FILE $token->as_is; } }}}

In keeping with wishes, and genies, and not trying people's patience, I ask my third and last question on this code...Is there a way to put a token back into the stream so that I can keep the file's title text?

UPDATE:Taking a step or two back from this I realized a simple answer to my own question. I just added a print FILE $title; statement in after the regex and it did what I needed. I was so wrapped up in parsing the HTML that I had lost the thought that I could use another approach. Thanks, Monks, as always for your assistance.


In reply to Re: Loop behavior with HTML::TokeParser::Simple by WhiteBird
in thread Loop behavior with HTML::TokeParser::Simple by WhiteBird

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-06-26 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (86 votes). Check out past polls.

    Notices?