Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create Files in foreach loop

Posted on 2012-04-05
4
Medium Priority
?
322 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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 …
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

618 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