Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regarding file outputs to different files 1

Status
Not open for further replies.

johndoe3344

Programmer
Jul 5, 2007
13
US
I have a question on how to print to different files, say, out.1.txt, out.2.txt, etc.

If i have

open (OUTP, ">out.txt")
print OUTP "$_";

in some sort of loop, then out.txt only stores the information in the last iteration of the loop because all the other attempts were overwritten. What I'm looking for is for the first iteration to print to out.1.txt, the second iteration to out.2.txt, and so on.

Could someone tell me how to do this or point me to a webpage which explains it?
 
Maybe something like this?
Code:
$x = "0";
for $var (@array) {
$out = "out" . $x . ".txt";
open(OUT, ">$out")
print OUT "$var\n";
close(OUT);
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Does the 'for' construct here serve the same purpose as a 'foreach'? Also do I need to predefine certain values in @array? Thanks.
 
for and foreach are the same thing..

Maybe we don't have a clear understanding of what you are trying to do. I thought you wanted to know how to loop through your data and print each line out to a different file (my code had a flaw in it anyway so I'll repost it with the fix). Please try and explain exactly what you are trying to accomplish
Code:
$x = "0";
for $var (@array) {
$out = "out" . $x . ".txt";
open(OUT, ">$out")
print OUT "$var\n";
close(OUT);
$x++;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
why don't you just use append mode?

open(OUT,">>file.txt");
print OUT $_;
close(OUT);





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I guess I assumed he knew about append and really wanted stuff in different files.. but at this point.. who knows :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top