[ABE.pm] rolling a loop

Faber Fedor faber at linuxnj.com
Tue Oct 19 10:18:46 CDT 2004


Hi guys,

I'm trying to roll a loop (as opposed to "unrolling a loop") but I can't
figure out the details.  Here's a sample of what I've got:

    $table="mytable1"; $Vfield="field1"; $dum="sum1"; $col="col1";
    $stmt = "update $table set $field = $sum where $col='Y'";
    $dbh->do($stmt);

    $table="mytable2"; $field="field2"; $dum="sum2"; $col="col2";
    $stmt = "update $table set $field = $sum where $col='Y'";
    $dbh->do($stmt);

    $table="mytable3"; $field="field3"; $dum="sum3"; $col="col3";
    $stmt = "update $table set $field = $sum where $col='Y'";
    $dbh->do($stmt);

etc.

What I want to do is something like this

    @data = ("mytable1", "field1", "sum1", "col1",
	     "mytable2", "field2", "sum2", "col2",
	     "mytable3", "field3", "sum3", "col3",
    );

    foreach my ($table, $field, $sum, $col) (@data) {
	$stmt = "update $table set $field = $sum where $col='Y'";
        $dbh->do($stmt);
    }

But that doesn't work in Perl. I think I spent too much time in Python.
What's te proper Perl idiom/technique to roll this loop up?

-- 
 
Regards,
 
Faber                     

Linux New Jersey: Open Source Solutions for New Jersey
http://www.linuxnj.com





More information about the ABE-pm mailing list