in reply to foreach loop not retrieving all data.

I'll give you a clue. Your problem is here:

my @trips= $p->findnodes( '//table[@id="tblFYCXML_Itin"]');

How many tables with id="tblFYCXML_Itin" do you expect the page to contain?

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: foreach my $question (@perlmonks){}
by marcoss (Novice) on Jun 19, 2012 at 10:10 UTC

    Hi, in that page there's only 1 table with that ID...so, I'm expecting the foreach loop to show me every h2, or every div[@class="something"]/a... or whatever I need to extract, such as departure dates, ship names, prices, duration..etc. mmmmm...I still can't see where the mistake is... perhaps one more clue...? Thanks!

      But that's not what the code says... Walk through it with me.

      my @trips= $p->findnodes( '//table[@id="tblFYCXML_Itin"]'); # So there's exactly one table with that id. # So @trips contains now exactly one node, that node being that one ta +ble. # You still with me? # If not, try it: print "There is/are ", scalar(@trips), " nodes in \@trips.\n";

      Okay. And then:

      foreach my $trip (@trips){

      You see it? Look at that line again. See it now? Look again until you do.

      For each element of @trips, an array of which we just established that it has exactly one element, anyway, so for each element of that set of one element,, you want to do something. And you get a result like it runs the loop only exactly one time. Hmm, boggles the mind, don't it :)

      If, at this point, you still really need another clue? Try finding those nodes that you want to loop over, and loop over them, instead of trying to loop over something that you know only occurs once.

        Ok!!, got it... it's working now. I replaced the table for the nodes that are repeated and contain the info I need. I thought the array line was ok, that's why I've been wasting time with the foreach. Thank you so much!!!

        marcos

      Well, there's your problem!

      This is essentially your code

      my @tables = ( { 'h2' => [ .. ], 'div/a' => [ .. ], }, ); for my $table( @tables ){ my $oneh2 = $table->['h2']->[0]; my $onediv = $table->['div/a']->[0]; }

      You're asking why this doesn't look for multiple h2s or divs -- do you see why it doesn't?

        Yes!!, I finally got it working...I feel st_p_d, jejeje. Just a question. Should I update my initial post with the right code or do something like "solved"?

        Thanks!!