http://www.perlmonks.org?node_id=1039039


in reply to Help with a loop

A few comments:

You should be using: use warnings; and use strict;, because they will reveal a lot of errors (e.g., Scalar value @jobvar_list[$n] better written as $jobvar_list[$n] at ...

I added these and made the necessary changes (including declaring everything with my ... and re ran it (after commenting out the subroutine that gets called) and it seems to work fine:

#!/usr/bin/env perl use strict; use warnings; my @jobvar_list = (); my @jobname_list = (); my @jobnice_list = (); my $n = 0; $jobvar_list[$n] = "senior"; $jobname_list[$n] = " Senior"; $n++; $jobvar_list[$n] = "exec"; $jobname_list[$n] = " Executive"; $n++; my $quick_build = "listing"; my $quick_jobtype = "exec"; print "job types...<br>\n"; for ( my $st = 0 ; $st <= @jobvar_list - 1 ; $st++ ) { my $temp_jobvar = $jobvar_list[$st]; print "$temp_jobvar, $quick_jobtype...<br>\n"; if ( ( $quick_jobtype && ( $quick_jobtype eq $temp_jobvar ) ) || ( !$quick_jobtype ) ) { my $jobvar = $temp_jobvar; my $jobname = $jobname_list[$st]; my $groupfile = $jobvar; my $grouptitle = $jobname; my $sortby = " WHERE jobtype like '%$jobvar%' and active='yes' AND jobstatus!='susp +end' ORDER BY dateadded DESC, jobtitle ASC"; print "$sortby"; print "starting build lists<br>\n"; my $building_cat = "yes"; # &Build_Lists_Both; } # end if ($cat1 eq $cat_db_var) } # end for ($st=1; $st<=$jobvar_list; $st++) __END__ job types...<br> senior, exec...<br> exec, exec...<br> WHERE jobtype like '%exec%' and active='yes' AND jobstatus!='suspend' + ORDER BY dateadded DESC, jobtitle ASCstarting build lists<br>

Also, you can write your for loop in a more readable manner like this:

#OLD: for ( my $st = 0 ; $st <= @jobvar_list - 1 ; $st++ ) { for my $st ( 0 .. $#jobvar_list ) {