Google Groups Home
Help | Sign in
Message from discussion write 10 files
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
A. Sinan Unur  
View profile
 More options May 16, 6:41 am
Newsgroups: comp.lang.perl.misc
From: "A. Sinan Unur" <1...@llenroc.ude.invalid>
Date: Fri, 16 May 2008 10:41:09 GMT
Local: Fri, May 16 2008 6:41 am
Subject: Re: write 10 files
"Ela" <e...@yantai.org> wrote in
news:g0jl3m$cj$1@ijustice.itsc.cuhk.edu.hk:

> 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 <1...@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google