Solved

Create Files in foreach loop

Posted on 2012-04-05
4
313 Views
Last Modified: 2012-04-05
Hi,
I need to create a file in foreach loop .
Its like in my code i need to manipulate data to appear in a certain way and its going through loops of foreach as shown below.

foreach $a(@a){
   foreach $b(@b){
       foreach $c(@c){
            foreach $d(@d){
                foreach $e(@e){
                                           print hash{$a}{$i}{name}
                                           print hash{$b}{$i}{name}
                                           .......
                                           if (){
                                           ........
                                           }
                                      }
                                  }
                          }
               }
       }

Now i need to create a file within loop @a so that i can access all the elements of @b for each $a.

Please help !
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
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37814853
What should be in the file?  What are you doing with the file?  Do is it a different filename each time through the loop?

It looks like you are already accessing all the elements of @b for each $a, so I'm not sure where the file comes in,
0
 

Author Comment

by:erum87
ID: 37815027
I need to create a file for each element $b under $a. I need just one filehandler as only one file will be processed each time.
Illutrating it with a sample code to illustrate better-

foreach $a(@a){
¿   foreach $b(@b){
      for($x=0;$x<=20;$x++){
      open(fh,$nm.txt);
¿       foreach $c(@c){
¿            foreach $d(@d){¿
                foreach $e(@e){¿ 
                                          print hash{$a}{$i}{name}¿ 
                                          print hash{$b}{$i}{name}¿ 
                                          .......¿   
                                       if (){¿ 
                                         ........¿   
                                        }¿   
                                  }¿     
                             }¿     
                    }¿         
     }
      }¿       }
0
 

Author Comment

by:erum87
ID: 37815038
My dilemma is .. that the file name $nm actually needs to be the name of elements in $b. Say if @b = {apple,orage,grapes,kiwi} the file name that is generated should be apple.txt, orange.txt, grapes.txt, kiwi.txt .
 And the content of the files that is being written is print statements that are used to print the data out in certain format.
So it has to go into loop a, then into loop b, process data of loop c,d,e respectively, print it out in the files, which has names that of elements in array b.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 37815097
open(fh,">$b.txt") or die "$b.txt $!";

 print fh $hash{$a}{$i}{name};
0

Featured Post

Industry Leaders: 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

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…
Six Sigma Control Plans

691 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