Solved

Perl - foreach loop and else statement

Posted on 2010-09-06
3
953 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now