Solved

Create Files in foreach loop

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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