Solved

Printing header just once ina a loop

Posted on 2012-04-09
3
200 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
[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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 105
Perl, group, sort, count question 6 150
Need help with bash and/or perl commands on OS X Terminal 9 111
Port 80 requests 16 103
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

749 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