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 Mike Lewis 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++;
}

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

adding the ">" sign made it work now.
thanks kevinADC for the solution. it has been a frustating afternoon.

sorry for the late response. i updated my perl version from 5.8.8 to 5.10 just to rule out that possibility, and that took this long.



 
ahh, good catch, don't know how I left that off when posting the code. While I'm here, for what it's worth I noticed the code can be simplified a little:

Code:
![red]/[/red][purple]usr[/purple][red]/[/red]bin/perl
[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]$y[/blue] = [fuchsia]100[/fuchsia][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$i[/blue] [red]([/red][fuchsia]1..100[/fuchsia][red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [black][b]my[/b][/black] [blue]$file[/blue], [red]"[/red][purple]>[blue]$i[/blue]-times-fileA.txt[/purple][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]$i[/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][red];[/red]



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

Part and Inventory Search

Sponsor

Back
Top