write 10 files

E

Ela

The following codes are not accepted by Perl and even string concatenation
doesn't work. Any suggestions?


#!/usr/bin/perl

$infile = $ARGV[0];
foreach $k (0..10) {
$outfile = $infile . $k;
open (OFP$k, ">$outfile");
}

$i = $size/10000;
print OFP$i "something"; #0-10k, 10-20k, .... 90-100k...
 
A

A. Sinan Unur

The following codes are not accepted by Perl and even string
concatenation doesn't work.

Doesn't work is not a good problem description.
Any suggestions?

See below.
#!/usr/bin/perl

use strict;
use warnings;
$infile = $ARGV[0];
foreach $k (0..10) {

You realize that loops 11 times, right?
$outfile = $infile . $k;
open (OFP$k, ">$outfile");

First off, you should check if open succeeded. Second, what makes you
think you can use OFP$k where Perl expects a filehandle.

This looks like a very poor attempt at using symbolic file handles (if
such a thing even exsits, I don't know).

When you find yourself wanting to index something using an integer, you
should use an array.
}

$i = $size/10000;
print OFP$i "something"; #0-10k, 10-20k, .... 90-100k...

#!/usr/bin/perl

use strict;
use warnings;

my ($prefix) = @ARGV;
die "No prefix specified\n" unless defined $prefix;

my @out;

for my $i ( 0 .. 9 ) {
my $name = "${prefix}${i}";
if ( open my $fh, '>', $name ) {
push @out_h, { name => $name, handle => $fh };
}
else {
warn "Error opening '$name': $!\n";
}
}

for my $file ( @out ) {
my $handle = $file->{handle};
my $name = $file->{name};
print $handle "This is $name\n";
unless ( close $handle ) {
warn "Error closing '$name': $!";
}
undef $file->{handle};
}

__END__


Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
B

Ben Morrow

Quoth "A. Sinan Unur said:
First off, you should check if open succeeded. Second, what makes you
think you can use OFP$k where Perl expects a filehandle.

This looks like a very poor attempt at using symbolic file handles (if
such a thing even exsits, I don't know).

Yes, it does. A filehandle is just an unquoted string, so

open "OFP$k", ">$outfile";

'works', FSVO.

(I'm leaving this bit in just in case anyone gets the wrong idea from
this post... :) )
When you find yourself wanting to index something using an integer, you
should use an array.

Ben
 
E

Ela

Yes, it does. A filehandle is just an unquoted string, so

open "OFP$k", ">$outfile";

'works', FSVO.

(I'm leaving this bit in just in case anyone gets the wrong idea from
this post... :) )

Does "works" mean it does not generate error but still cannot achieve the
effect of printing into different files? Because I'm able to open empty
file1, file2, ..., file10 but nothing is printed into them.
 
B

Bill H

The following codes are not accepted by Perl and even string concatenation
doesn't work. Any suggestions?

#!/usr/bin/perl

$infile = $ARGV[0];
foreach $k (0..10) {
    $outfile = $infile . $k;
    open (OFP$k, ">$outfile");

}

$i = $size/10000;
print OFP$i "something"; #0-10k, 10-20k, .... 90-100k...

Unless $size is always evenly divisible by 10000 then $i will not be
the number you expect it to be (use $i = int($size/10000); instead)
and it will never print into one of the file handles you have open.
(for example, $size = 11,000 then $i = 11000/10000 will equal 1.1, not
the "1" you expect.)

Bill H
 
E

Ela

$i = $size/10000;
print OFP$i "something"; #0-10k, 10-20k, .... 90-100k...

Unless $size is always evenly divisible by 10000 then $i will not be
the number you expect it to be (use $i = int($size/10000); instead)
and it will never print into one of the file handles you have open.
(for example, $size = 11,000 then $i = 11000/10000 will equal 1.1, not
the "1" you expect.)

Bill H

Yes, u a right. and I've already added int ($size/10000)
 
E

Ela

use strict;
use warnings;

my @fh;
my $k = 0;

open $fh[$k],'>','/tmp/test.txt' or die $!;
print {$fh[$k]} "Hello\n" or die $!;
close $fh[$k] or die $!;

Frank

Tested and confirmed to work, but also thank Sinan's great efforts. Also the
reminder from Bill. And lastly, Ben. He always helps me a lot.
 
A

A. Sinan Unur

Yes, it does. A filehandle is just an unquoted string, so

open "OFP$k", ">$outfile";

'works', FSVO.

(I'm leaving this bit in just in case anyone gets the wrong idea from
this post... :) )

Thank you. I should have checked that myself. I am hoping that the OP
dropped the use of "OFP$k" in favor of $OFP[$k].

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top