Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: I need a different continue

by hmerrill (Friar)
on Dec 09, 2004 at 18:19 UTC ( [id://413639] : note . print w/replies, xml ) Need Help??


in reply to I need a different continue

What is your loop based on? Is it like the above 0..2, or is it data driven? Can you post a part of the loop here to show the structure?

I don't know of another algorithm or module that does what you're looking for, but if you describe the loop in more detail, I (or someone else) might know of an approach you haven't thought of yet.

Replies are listed 'Best First'.
Re^2: I need a different continue
by DrWhy (Chaplain) on Dec 09, 2004 at 18:48 UTC
    Here's a simplified version of what I have now.
    lock_db_tables(); # lock tables and/or start a transaction # in a database independent way # some preparation that requires a database lock my $firsttime = 1; foreach my $id (@list_o_object_ids) { lock_db_tables() unless $firsttime; $firsttime = 0; my $obj = new Thingy($id); # gets stuff from db # make changes, save back to db. unlock_tables(); }
    The pre-loop code and the first loop iteration need to be in the same transaction. Subsequent loop iterations need to each be their own transaction.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      I guess it's the unlock_tables() that you want to run on all except the last iteration, right? you can move it to the beginning of the loop and skip it in the first iteration:

      my $firsttime = 1; foreach my $id (@list_o_object_ids) { unless ($firsttime) { unlock_tables(); lock_db_tables(); } else { $firsttime = 0; } my $obj = new Thingy($id); # gets stuff from db # make changes, save back to db. }

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

        No, that's not right. I want to lock the tables on all but the first iteration. Or to view this problem another way, I want to do a heck of a lot (about 1100 lines) of work the first time through the loop that is not repeated subsequent times. Right now, the first-loop-specific stuff is not in the loop at all, and due to it's size I'd prefer to keep it outside the loop. This is not a problem except that the pre-loop work and the first loop iteration must be within a single database transaction, while each subsequent loop is also it's own transaction.

        As I've already shown, (and you have shown a slightly different way) this problem can be solved with a semaphore variable and a check of the sempahore each trip through the loop. I just don't like that solution because it seems messy and wonder if there is a cleaner way. I was hoping to find some syntacical thingy that behaves kind of like a join, but with blocks of code rather than array elements. Here's some pseudo code to demonstrate what I had in mind.

        lock_db_tables(); # lock tables and/or start a transaction # in a database independent way # 1100 lines of preparation that requires a database lock # must be in same transaction as first iteration of loop # below foreach my $id (@list_o_object_ids) { my $obj = new Thingy($id); # gets stuff from db # make changes, save back to db. (about 900 lines in my case) unlock_tables(); } join { lock_db_tables(); }

        The lock_db_tables() within the join block would be executed between each loop iteration, but not before the first iteration, nor after the last iteration.

        --DrWhy

        "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."