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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through array 1

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
Hi! all, i am trying to print 100 files with 100 names, just the names, ('file-A.txt' or 'file-B.txt') in each file. in file 1, fileA will be printed 1 time and fileB 99 times, and in file 2, 2 times fileA and 98 times fileB and so on.

here is what i have done sofar. the program works but only prints one file and does not loop through. please suggest a working logic.

thanks in advance

#!/usr/bin/perl -w

my ($count, $counts);
$x=1;
$y=100-$x;
open(FILEW1, ">$x-times-fileA.txt") || die "couldn't create the file\n";

unless ($x==100){
$count=1;
while ($count<= $x) {
print FILEW1"file-A.txt\n";
$count++;
}

$counts=1;
while ($counts<=$y) {
print FILEW1"file-B.txt\n";
$counts++;

}
$x++;
}

 
First of all, you have the file open to overwrite, not append. Use ">>", not ">".
 
First of all, you have the file open to overwrite, not append. Use ">>", not ">".

Hi! AMiSM, As i wrote before, i want 100 files printed. with '$x' prefix. using ">>" wont solve it.
 
You'll have to close and reopen the file handle a hundred times.

Code:
for ( 1..100 ) {
     open ( FIL, ">$_-times-fileA.txt");
     print FIL "stuff-to-be-printed\n";
     close FIL;
     }
 
Hi! AMiSM, thank you for the respose. i am not clear how to incorporate the suggested file handle. is this how you want me to do it.

#!/usr/bin/perl -w

my ($count, $counts);
$x=1;
$y=100-$x;


for ( 1..100 ) {
open ( FIL, ">$x-times-fileA.txt");

unless ($x==100){
$count=1;
while ($count<= $x) {
print FILEW1"file-A.txt\n";
$count++;
}

$counts=1;
while ($counts<=$y) {
print FILEW1"file-B.txt\n";
$counts++;

}
$x++;
}

close FIL;
}

this one runs with out a error but prints a sigle file again. may be i am missing some thing here. sorry



 
Run the code I posted by itself to get an idea how file handles work.
 

Right, your code runs and creates 100 files.

My problem here is " in file 1, fileA will be printed 1 time and fileB 99 times, and in file 2, 2 times fileA and 98 times fileB and so on."


please run the code i posted first time and open the out file. you will see that "file-A.txt" is printed once and "file-B.txt" 99 times.

but i am not able to print the next 99 files with decreasing "file-A.txt" and increasing "file-B.txt" untill the last file has only "file-B.txt" printed 100 times.

thanks
 
Is this school work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Certainly sounds like it.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
What is the business need?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 

Ok, i work in genetics area. i am generating 100 combination files with two sequences, fileA and fileB (in percentages from 1 to 100 of fileA (sequence A) and will be randomizing them. then i want to extract a set number of sequences from each file (out of 100 entries) and want to generate an incidence curve. there are other parameters but this is the summary of it.

thanks
 
Cool. So if I understand then the solution is to have another for loop outside that controls the start value of $x in the inner loop.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
this might work:

[code
Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$x[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$y[/blue] = [fuchsia]100[/fuchsia][red];[/red]

[olive][b]for[/b][/olive] [red]([/red] [fuchsia]1..100[/fuchsia] [red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red] [black][b]my[/b][/black] [blue]$file[/blue], [red]"[/red][purple][blue]@[/blue]{[++[blue]$x[/blue]]}-times-fileA.txt[/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$file[/blue] [red]"[/red][purple]file-A.txt[purple][b]\n[/b][/purple][/purple][red]"[/red] x [blue]$x[/blue][red];[/red]
   [black][b]print[/b][/black] [blue]$file[/blue] [red]"[/red][purple]file-B.txt[purple][b]\n[/b][/purple][/purple][red]"[/red] x [blue]$y[/blue]--[red];[/red]
   [url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]$file[/blue][red];[/red]

[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple]finished[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
oops, maybe I missed that you want the files named alternately fileA and fileB but that should be easy to fix.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
alternates between fileA and fileB:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$x[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$y[/blue] = [fuchsia]100[/fuchsia][red];[/red]

[olive][b]for[/b][/olive] [red]([/red] [fuchsia]1..100[/fuchsia] [red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red] [black][b]my[/b][/black] [blue]$file[/blue], [red]"[/red][purple][blue]@[/blue]{[++[blue]$x[/blue]]}-times-file[blue]@[/blue]{[[blue]$x[/blue]%2 == 0 ? B : A]}.txt[/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$file[/blue] [red]"[/red][purple]file-A.txt[purple][b]\n[/b][/purple][/purple][red]"[/red] x [blue]$x[/blue][red];[/red]
   [black][b]print[/b][/black] [blue]$file[/blue] [red]"[/red][purple]file-B.txt[purple][b]\n[/b][/purple][/purple][red]"[/red] x [blue]$y[/blue]--[red];[/red]
   [url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]$file[/blue][red];[/red]

[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple]finished[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thanks KevinADC. i will go with the first solution you posted. only fileA in the file name is what i want.
however, when i run your code, it give me the error massage,

"no such file or directory at line 8."



 
hmmm.... so no files are being created? The code works OK for me, both versions, but I did append a path before the filename:

Code:
open ( my $file, "c:/test/@{[++$x]}-times-file@{[$x%2 == 0 ? B : A]}.txt") or die "$!";

If you are on Windows still use forward slashes in your directory paths. Don't put a backslash before the @ symbol.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
or like this:

Code:
open ( my $file, "@{[++$x]}-times-fileA.txt") or die "$!";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have to run, I'll check back later.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top