Solved

Perl - foreach loop and else statement

Posted on 2010-09-06
3
1,028 Views
Last Modified: 2012-05-10
I have the foreach loop in a sub I use, I cannot get the "else" to print if $match is not found, the way I have it below works ok other than I cannot get the  print to work for the else.

Any help would be much appreciated.

foreach my $regions (@regions) {
 open(GCSEA,"/usr/local/bin/adsearch xxx samaccountname=$sam-$regions samaccountname | ");
  while(<GCSEA>) {
   if (/^sAMAccountName: (.*?)/) {
    (my $match) = (/sAMAccountName: (.*)/);
if (defined($match)) {
   print "\t$regions-",$match,"\t";
} else {
   print $regions,"-NotInAd\n";
}
   } 
 }
}

Open in new window

0
Comment
Question by:bt707
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 300 total points
ID: 33613062
get your indentation sorted, it will make life easier to read

Now looking at your code, have you checked the first if? there is no else on that. So your code is only going to print something if it passed the first if condition
Can you confirm it is going pass the first if?

the attached code has been indented properly with the extra else added on the first if

foreach my $regions (@regions) {
	open(GCSEA,"/usr/local/bin/adsearch xxx samaccountname=$sam-$regions samaccountname | ");
	while(<GCSEA>) {
		if (/^sAMAccountName: (.*?)/) {
			(my $match) = (/sAMAccountName: (.*)/);
			if (defined($match)) {
				print "\t$regions-",$match,"\t";
			} else {
				print $regions,"-NotInAd\n";
			}
		} else {
			print "Dodgy\n";
		}
	}
}

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 200 total points
ID: 33614609
Note that both your first and second if statement are looking for the same thing.  So, if it matches the first time, it will match the second time - and you'll never get to the else for the second if.  In rockiroads code, this would print "Dodgy".

0
 

Author Closing Comment

by:bt707
ID: 33617490
Thanks to both for the help, I figured out why I cannot get what I want, it is basically in the first condition and also in the way I put in the if/else statements which both pointed out. I got it working now.

Thanks,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question