Solved

Printing header just once ina a loop

Posted on 2012-04-09
3
197 Views
Last Modified: 2012-05-10
Hi i have a script that should look like this-
###########################################################
Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=1 , env= if its not the default env display the other env here)
#print files relevant to milestone 1 under that plan under that chapter
filename1
filename2
filename3
filename4
(milestone=2 , env= if its not the default env display the other env here)
filename5
filename6
filename7
(milestone=3 , env= if its not the default env display the other env here)
filename8
(milestone=4 , env= if its not the default env display the other env here)
filename9
filename10
filename11
filename12
filename13
filename14
#############################################################

but instead looks like

############################################################
Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=1 , env= if its not the default env display the other env here)
#print files relevant to milestone 1 under that plan under that chapter
filename1
filename2
filename3
filename4

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=2 , env= if its not the default env display the other env here)
filename5
filename6
filename7

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=3 , env= if its not the default env display the other env here)
filename8

Plan: the plan its accessing
Chapter: the chapter under that plan
Owner:Default owner (there can be multiple owners)
Platfrom: Default platform
Env : Default environemt

(milestone=4 , env= if its not the default env display the other env here)
filename9
filename10
filename11
filename12
filename13
filename14
##############################################################

So the body of my code looks like this
##############################################################
my $flag = 0;
foreach $plan(@plan){
      foreach $chap(@chap){
            foreach $mlst (@milestone){
                  foreach $plt (@platform){
                  for (i=0;i<=200;i++ )
                  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
{
if ($flag ==0){
print "Plan= $plan";
print "Chapter=$chap";
print "Owner=$owner"; #processed in a subroutine
print "Platform=$plt";
print "(Milestone = $mlst , env: $en)";
}
print "$some{$hash}{$of}{filename}";
$flag = 1;
} } }  } }...
###########################################################

Please help me fix it !!
0
Comment
Question by:erum87
  • 2
3 Comments
 
LVL 12

Expert Comment

by:Amick
ID: 37824671
If I understand what you're trying to do this may work:

foreach $plan(@plan){
	print "Plan= $plan";
	foreach $chap(@chap){
		print "Chapter=$chap";
		print "Owner=$owner"; #processed in a subroutine
		print "Platform=$plt";
            foreach $mlst (@milestone){
		print "(Milestone = $mlst , env: $en)";
                  foreach $plt (@platform){
			  for (i=0;i<=200;i++ )
			  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
				print "$some{$hash}{$of}{filename}";
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:erum87
ID: 37824791
I'm sorry but it does not work :(

It prints the header for all the iterations its going through. And i just want it printed once at the top for that particular values relevant to those elements in the loop.
0
 
LVL 12

Accepted Solution

by:
Amick earned 500 total points
ID: 37829181
I'm sorry it didn't work for you.  This might be worth a try.

foreach $plan(@plan){
	if (($some{$hash}{$of}{plan} eq $plan)	print "Plan= $plan";
	foreach $chap(@chap){
	if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap){
		print "Chapter=$chap";
		print "Owner=$owner"; #processed in a subroutine
		print "Platform=$plt";
		}
            foreach $mlst (@milestone){
		if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst) print "(Milestone = $mlst , env: $en)";
                  foreach $plt (@platform){
			  for (i=0;i<=200;i++ )
			  if (($some{$hash}{$of}{plan} eq $plan) && ($some{$hash}{$of}{chap} eq $chap)&&($some{$hash}{$of}{mlst} eq $mlst)&&($some{$hash}{$of}{plt} eq $plt))
				print "$some{$hash}{$of}{filename}";
			}
		}
	}
}

Open in new window

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

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

25 Experts available now in Live!

Get 1:1 Help Now