Google Groups Home
Help | Sign in
write 10 files
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all
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
Ela  
View profile
 More options May 16, 5:45 am
Newsgroups: comp.lang.perl.misc
From: "Ela" <e...@yantai.org>
Date: Fri, 16 May 2008 17:45:21 +0800
Local: Fri, May 16 2008 5:45 am
Subject: write 10 files

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...

    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.
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.
Ben Morrow  
View profile
 More options May 16, 7:46 am
Newsgroups: comp.lang.perl.misc
From: Ben Morrow <b...@morrow.me.uk>
Date: Fri, 16 May 2008 12:46:48 +0100
Local: Fri, May 16 2008 7:46 am
Subject: Re: write 10 files

Quoth "A. Sinan Unur" <1...@llenroc.ude.invalid>:

> "Ela" <e...@yantai.org> wrote in
> news:g0jl3m$cj$1@ijustice.itsc.cuhk.edu.hk:

> >     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).

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

--
I touch the fire and it freezes me,                      [b...@morrow.me.uk]
I look into it and it's black.
Why can't I feel? My skin should crack and peel---
I want the fire back...                     Buffy, 'Once More With Feeling'


    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.
Ela  
View profile
 More options May 16, 9:37 am
Newsgroups: comp.lang.perl.misc
From: "Ela" <e...@yantai.org>
Date: Fri, 16 May 2008 21:37:20 +0800
Local: Fri, May 16 2008 9:37 am
Subject: Re: write 10 files

> 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.

    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.
Bill H  
View profile
 More options May 16, 10:05 am
Newsgroups: comp.lang.perl.misc
From: Bill H <b...@ts1000.us>
Date: Fri, 16 May 2008 07:05:39 -0700 (PDT)
Local: Fri, May 16 2008 10:05 am
Subject: Re: write 10 files
On May 16, 5:45 am, "Ela" <e...@yantai.org> wrote:

> 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


    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.
Ela  
View profile
 More options May 16, 10:59 am
Newsgroups: comp.lang.perl.misc
From: "Ela" <e...@yantai.org>
Date: Fri, 16 May 2008 22:59:10 +0800
Local: Fri, May 16 2008 10:59 am
Subject: Re: write 10 files

> $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)


    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.
Frank Seitz  
View profile
 More options May 16, 11:00 am
Newsgroups: comp.lang.perl.misc
From: Frank Seitz <devnull4...@web.de>
Date: Fri, 16 May 2008 17:00:24 +0200
Local: Fri, May 16 2008 11:00 am
Subject: Re: write 10 files

Ela wrote:
>>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.

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
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel


    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.
Discussion subject changed to "DONE: write 10 files" by Ela
Ela  
View profile
 More options May 16, 11:23 am
Newsgroups: comp.lang.perl.misc
From: "Ela" <e...@yantai.org>
Date: Fri, 16 May 2008 23:23:33 +0800
Local: Fri, May 16 2008 11:23 am
Subject: DONE: write 10 files

> 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.

    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.
Discussion subject changed to "write 10 files" by A. Sinan Unur
A. Sinan Unur  
View profile
 More options May 17, 7:34 am
Newsgroups: comp.lang.perl.misc
From: "A. Sinan Unur" <1...@llenroc.ude.invalid>
Date: Sat, 17 May 2008 11:34:32 GMT
Local: Sat, May 17 2008 7:34 am
Subject: Re: write 10 files
Ben Morrow <b...@morrow.me.uk> wrote in
news:8q80g5-hf4.ln1@osiris.mauzo.dyndns.org:

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 <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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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