Solved

Printing header just once ina a loop

Posted on 2012-04-09
3
196 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

16 Experts available now in Live!

Get 1:1 Help Now