Solved

Create Files in foreach loop

Posted on 2012-04-05
4
298 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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

11 Experts available now in Live!

Get 1:1 Help Now