in reply to Help with a loop

First rule of fixing broken perl code. Apply 'use strict;' and 'use warnings;'. This gives a lot of warnings and errors with your code, which would warrant sorting first.

It looks like you're doing a fairly substantial number of slightly horrible things. I would imagine that if you did use strict and warnings, your problem would vanish (or become obvious. For starters - populating an array by setting $n.

my @jobvar_list = ( "senior", "exec" );

Will do exactly the same thing. Even better, you could use a hash, so you never needed to iterate through one list, and reference another list.

my %jobvar_list = ( 'senior' => 'Senior', 'exec' => 'Exec' ); my $quick_jobtype = "exec"; foreach my $temp_jobvar ( keys %jobvar_list ) { print "$temp_jobvar, $jobvar_list{$temp_jobvar}\n"; if ( $temp_jobvar eq $quick_jobtype ) { print "$temp_jobvar matches $quick_jobtype"; my $sortby = " WHERE jobtype like '%$jobvar%' and active='yes' AND + jobstatus!='suspend' ORDER BY dateadded DESC, jobtitle ASC"; print $sortby; } }

I think it must be that 'if' clause that's causing you problems - what is it trying to do? It sounds like it's evaluating but never working, and therefore not running that 'sortby' and 'Build_Lists_Both' segments of code. Which is odd, because it seems to be doing so in that sample code you posted. Is there any chance that you've tidied your code up a bit, with the job title list? that 'eq' line means that the two variables have to match precisely - no whitespace or linefeeds. Because your code _does_ run as is.

Replies are listed 'Best First'.
Re^2: Help with a loop
by htmanning (Friar) on Jun 14, 2013 at 21:44 UTC
    Monks, Thanks so much! I didn't write this code originally, but edited it to fit what I needed, but I'm an amateur. Thanks for all the suggestions (most of which are above my head). Preceptor solved it for me with this: "that 'eq' line means that the two variables have to match precisely - no whitespace or linefeeds. " I changed it to:
    if (($quick_jobtype && ($quick_jobtype =~ /$temp_jobvar/)) || (!$quick +_jobtype)) {
    and IT WORKS! Once again the Monks have saved me. Thanks.

      As you're maintaining the code, a rewrite is probably a good idea - there really is nothing like code that's not quite correct to give you all manner of headaches down the line (despite working now)