my $html =<<'HTML'; <ul> <li>question 1</li> <li>question 2</li> </ul> <ul> <li>answer 1</li> <li>answer 2</li> </ul> <ul> <li>question 3</li> <li>question 4</li> </ul> <ul> <li>answer 3</li> <li>answer 4</li> </ul> HTML my(@questions, @answers); my @arr = \(@questions, @answers); while ($html =~ m{<ul> (.*?) </ul>}xmsg ) { my $list = $1; while ($list =~ m{<li> (.*?) </li>}xmsg ) { my $question_or_answer = $1; push @{$arr[0]}, $question_or_answer; } (@arr[0], @arr[1]) = (@arr[1], @arr[0]); } print "@{$arr[0]} \n"; print "@{$arr[1]} \n"; --output:-- question 1 question 2 question 3 question 4 answer 1 answer 2 answer 3 answer 4

However, there are modules on cpan, that use xpaths for instance, which allow you to pick out the elements of an html page without having to create your own regexes.

Also, removing newlines(\n) before you search the html page does not accomplish anything useful.

Also, when asking a question about html, instead of confusing the issue by trying to describe the html, it's much easier and clearer to post some sample html instead--rather than making everyone guess what the exact structure is.

I've tried to read books and online resources, but I tend to learn best by examples to "see how it works."

When you buy a computer book (yes, you have to buy a physical book so that you can scribble notes in the margins), you should always buy one with questions and answers at the end of each chapter, e.g. "Learning Perl 5th" (although if you are completely new to programming that my be too hard of a book). In addition, as you read a computer programming book, you should type out some 2-5 line examples to test if things really work the way the book says. You'd be surprised how much you can learn about the nuances of the concepts by doing that.

I think if you don't buy a beginning book, then your studies will be too haphazard. It takes work to learn a language. It's not all fun and games.


In reply to Re: Help with the push function by 7stud
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.