?
Solved

Create Files in foreach loop

Posted on 2012-04-05
4
Medium Priority
?
318 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 2000 total points
ID: 37815097
open(fh,">$b.txt") or die "$b.txt $!";

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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

771 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