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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |