PDA

View Full Version : help in preventing a loop


bazz
07-22-2005, 12:28 PM
Hi,

In an array there will be many Business names.

If the specific $client_reference is in the list, sub1 should run.

If the $client_reference is not in that list, I want to run the sub2.

Unfortunately, as I have written it, it prints the second sub for each of the files that do not eq $client_reference.

I can see why it does that but can't figure a way to print the second sub just once.

I tired flagging yesterday but it didn't work. probably my mistake so I'll try it again as it seems, to me, logical that it should do what I want.

Any pointers you may have would be very helpful this Friday.

here is the first code which repeats sub 2 as many times as there are files.


foreach my $key (sort @listOfFiles) {

unless ($key eq $client_reference) {
&new_client_details;
}
else {
&client_exists;
}


And here's the flagged code which gives the two subs together - the form to add a new client and also then, telling me that the client is already in the system. :rolleyes: (heh, heh, just realised the irony. That scenario is my 'H*ly Grail' :D )


foreach my $key (sort @listOfFiles) {

unless ($key eq $client_reference) {
&new_client_details;
}
else {
$exists = 1;
}

if ($exists = 1) {
&client_exists;
}




Bazz

mlseim
07-22-2005, 12:55 PM
Bazz ....

I'm thinking it has to do with the returning of the subroutine ...
like the logical flags are reset or something.

Try these out (see if either one makes a difference):

foreach my $key (sort @listOfFiles) {

unless ($key eq $client_reference) {
&new_client_details;
exit;
}
else {
&client_exists;
exit;
}

foreach my $key (sort @listOfFiles) {

if ($key ne $client_reference) {
&new_client_details;
}
elseif ($key eq $client_reference){
&client_exists;
}

bazz
07-22-2005, 03:03 PM
The exit; did it so I am now enjoying a happy Friday :)

Thanks mlseim. :thumbsup:


foreach my $key (sort @listOfFiles) {

if ($key eq $client_reference) {
&client_exists;
}
if ($key ne $client_reference) {
$noExist = 0;
if ($noExist == 0) {
&new_client_details;
exit;
}
}

bazz
07-23-2005, 05:16 PM
Right then, I am really confused.

I have tried to accommodate all scenarios; when both client_reference matches and does not match, $key.

But the following still prints out the add_new_client routine for all those files which alphabetically come before the file which does match. so I end up with an html page with perhaps 5 add new clients forms and then the text showing that one exists. :confused: :mad:
my $equal = "0";
my $notEqual = "0";

if ($client_reference eq $key) {
$equal = 1;
} elsif ($client_reference ne $key) {
$notEqual = 1;
} else {}

if ($notEqual == 1 && $equal == 0 ) { # if no files match the key run the 'add new client' sub.
&add_new_client;
} elsif ($equal == 1 && $notEqual == 1) { # if client_reference is equal AND not equal, to $key, show the 'client exists' sub.
&client_exists;
} else {}

Have any of you got a suggestion?

Bazz

bazz
07-23-2005, 08:27 PM
finally, finally, finally got it going Yay!!

Instead of using a sub to check for existing files, and then deciding which sub to run next, I used the one sub to output the form for new clients. I preceded that output with this conditional.


if ($client_reference eq $key) {
&client_exists;
} # end of conditional statement.

.....rest of sub


Really straightforward but it took quite a bit of head scratching. I really must read my sig more often :)

bazz