I tried to push @questions, $1 but it still isn't printing anything when I...

There's no mention of "push" in the code snippet following that statement, so we don't know what "tried to push @questions, $1 really means in this context. To recap the discussion so far: first you present some code, but no data, then some data (with a different problem) but no code. Dude, we cannot see the things in your head, or the things you see on your terminal but don't show us. Please try harder to be coherent.

The HTML document I'm trying to write this for is rather simple. It's a ordered list of quiz questions, followed by answers like this:

"Rather simple" seems apt for describing the markup (though it actually looks like it'll be prone to unpredictable variability) But the information content -- esp. the "Statement. Response to statement." case -- is only simple for human readers who know the language well enough that they can easily figure out which periods mark sentence boundaries (and which ones don't), and can tell the difference between a "statement" and a "response to a statement." (And BTW, have you noticed that sometimes an "answer" to a "question" contain another question?)

You don't say how much data of this sort you have to deal with, but if it gets to more than several dozen "statement. response." type cases, you can expect some edge cases that may need to be resolved by a human editor.

If you get acquainted with something like HTML::Parser or HTML::TokeParser, you'll have an easier time dealing with variable mark-up, so you can focus on the harder problem of parsing the information content (where the essential division you need to find might not be marked in any consistent or explicit way).

In pseudo-code terms, you probably want something like:

# setup the parser to capture the contents of all <li> chunks into @ar +ray, then foreach ( @array ) { if ( m{(.*?)<pre>(.*)</pre>}s # NB: "?" makes ".*" non-greedy or /(.*\?)\s+(\S.*)/s # NB: "\?" matches a literal qmark or /^([^.]+)\.\s+([^.]+\.)\s*$/s ) { push @questions, $1; push @answers, $2; } else { # you have a harder case to solve (might need a human) } }
If you have trouble with that, it'll be okay to start a new thread -- it'll be something other than "help with the push function"...

(updated last code snippet to correct a mistaken comment about "?")


In reply to Re^3: Help with the push function by graff
in thread Help with the push function by mistamutt

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.