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

Writing to an Array of Files 2

Status
Not open for further replies.

Dagon

MIS
Jan 30, 2002
2,301
GB
I was trying to do something similar to the UNIX split command. I am reading through a file and splitting the lines randomly between several output files. The number of output files is determined at run time by a UNIX environment variable.

I was trying to do it by maintaining a line count and then using modulus of that with the number of output files to determine which file to write to. The problem is with how to set up an array of file names and write to the correct one using a variable file handle.

So far I've got:

Code:
#!/usr/bin/perl

$no_part=$ENV{'_partitions'};
open(INPUT, $ENV{'_inputFile'});

for ($i=1; $i <= $no_part; ++$i)
{
  $output[$i] = "filehandle";
  $FilePath = "$ENV{'_outputFileStub'}" . "$i";
  open($output[$i], ">$FilePath")  or die $!;
}

# set line counter
$lc = 1;

while ($line = <INPUT>)
{
   chomp($line);
   $modval=($lc % $no_part)+1;
   $outfile=$output[$modval];
   print  $outfile $line, "\n";
   print  $modval, "\n";
   ++$lc;
}
close(INPUT);

This opens the files correctly, but the write dumps data only to the last opened file.
 
Didn't you just open up all your file handles with the same name of "filehandle"?
Code:
for ($i=1; $i <= $no_part; ++$i)
{
  $output[$i] = "filehandle";
  $FilePath = "$ENV{'_outputFileStub'}" . "$i";
  open($output[$i], ">$FilePath")  or die $!;
}

maybe try
Code:
for ($i=1; $i <= $no_part; ++$i)
{
  $output[$i] = "filehandle$i";
  $FilePath = "$ENV{'_outputFileStub'}" . "$i";
  open($output[$i], ">$FilePath")  or die $!;
}

Just a suggestion.. not tested or anything

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
I thought that was probably the case, but I'm new to PERL and I'm struggling a bit at the moment. I thought the line identified it as a file handle type or something. I'll give your suggestion a try.
 
Not sure how wise some of this is, but I think this will work assuming your ENV variables are properly defined and don't have any dangerous characters in them:

Code:
[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]
[black][b]use[/b][/black] [green]IO::File[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$no_part[/blue] = [blue]$ENV[/blue][red]{[/red][red]'[/red][purple]_partitions[/purple][red]'[/red][red]}[/red][red];[/red]
[black][b]my[/b][/black] [blue]@output[/blue] = [red]([/red][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue]=[fuchsia]1[/fuchsia][red];[/red] [blue]$i[/blue] <= [blue]$no_part[/blue][red];[/red] ++[blue]$i[/blue][red])[/red]
[red]{[/red]
   [black][b]my[/b][/black] [blue]$fh[/blue] = new IO::File [red]"[/red][purple]>[blue]$ENV[/blue]{'_partitions'}[blue]$i[/blue][/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@output[/blue],[blue]$fh[/blue][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]INPUT, [blue]$ENV[/blue][red]{[/red][red]'[/red][purple]_inputFile[/purple][red]'[/red][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]
[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$line[/blue] = <INPUT>[red])[/red]
[red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
   [black][b]my[/b][/black] [blue]$modval[/blue] = [red]([/red][blue]$.[/blue] [blue]%[/blue] [blue]$no_part[/blue][red])[/red]+[fuchsia]1[/fuchsia][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url]  [red]{[/red] [blue]$output[/blue][red][[/red][blue]$modval[/blue][red]][/red] [red]}[/red] [red]"[/red][purple][blue]$line[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [black][b]print[/b][/black]  [blue]$modval[/blue], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[red]}[/red]
[url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]@output[/blue][red];[/red] [gray][i]#should close all open files [/i][/gray]
[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]
Core (perl 5.8.8) Modules used :
[ul]
[li]IO::File - supply object methods for filehandles[/li]
[/ul]
[/tt]

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

Part and Inventory Search

Sponsor

Back
Top