#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593

    Incrementing within a foreach


    I am using HTML::SimpleParse to process some HTML. I have a foreach loop to process the nodes. However, under certain circumstances I need to increment from within the loop to process the next node rather than the current one.
    Code:
    foreach my $node ($p->tree) {
       #some processing
       if (<some condition>) {
          <increment>  # somehow get the next value of $node
       }
       # more processing
    }
    I can't just use 'next' as that would require a host of 'if' blocks to keep track of which node was skipped/being processed.
    TIA
    Last edited by gw1500se; December 12th, 2012 at 01:09 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Why do you believe that you need "host of 'if' blocks" if you use next to move the the next host?

    If you need to track which nodes get skipped, you can simply use a hash or an array to store that info.

    Code:
    my @skipped;
    
    NODE: foreach my $node ($p->tree) {
       #some processing
       if (<some condition>) {
          push @skipped, $node;
          next NODE;
       }
       # more processing
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    Perhaps I am missing something about 'next'. Are you saying that 'next' with a label behaves differently and does not end that iteration through the loop but continues with the new value? What happens if there are no more values? A run time error or does it behave like 'last'?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    What values are you referring to?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    The values of $node.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Originally Posted by gw1500se
    The values of $node.
    The value of $node is an anonymous hash with (at least) three data members.

    If you're referring to those, then you'll need an inner loop, which your sample code doesn't have, and within that loop you can use 'next' with a label to skip over any of those data members as needed.

    If that's not what you're looking for, then you'll need to provide more detailed explanation and code example.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    Not quite and my apologies for not being clear. I use '$p->execute($node)' to get the HTML tag/text. If that tag/text contains certain information then I need to get $node to have the next member of $p->tree (that is what I meant by the next value of $node) and continue processing that iteration of the loop. That is my stumbling block. How do I get $node to have the next member of $p->tree without ending that iteration of the loop.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo