generic variable name possible?

E

ela

While I succeeded in producing generic file names, I found that I cannot
name the file pointer ($PROBLEMFP in the following codes) generically. I
have to keep unknown number of file pointers (depending on the number of
lines in modellist, not known beforehand) open and therefore I have to
create "n" $PROBLEMFP's. Is it possible to achieve this in Perl?



#!/usr/bin/perl
my ( $iteration, $modellist ) = @ARGV;

open( my $FPM, '<', "$modellist") or die "could not open '$modellist' $!";

$lineM = <$FPM>;
@models = split(/\t/, $lineM);

foreach (@models) {
$modelscript = $_ . "_script.txt";
$modellog = $_ . ".log";

##########################
open( my $PROBLEMFP, '>', "$modelscript") or die "could not open
'$modelscript' $!";
}
 
K

kun niu

While I succeeded in producing generic file names, I found that I cannot
name the file pointer ($PROBLEMFP in the following codes) generically. I
have to keep unknown number of file pointers (depending on the number of
lines in modellist, not known beforehand) open and therefore I have to
create "n" $PROBLEMFP's. Is it possible to achieve this in Perl?

#!/usr/bin/perl
my ( $iteration, $modellist ) = @ARGV;

open( my $FPM, '<', "$modellist") or die "could not open '$modellist' $!";

$lineM = <$FPM>;
@models = split(/\t/, $lineM);

foreach (@models) {
$modelscript = $_ . "_script.txt";
$modellog = $_ . ".log";

##########################
open( my $PROBLEMFP, '>', "$modelscript") or die "could not open
'$modelscript' $!";

}

Here's part of my code:
$file = "test.txt";
@test = (1,2);
foreach(@test)
{
open($_, ">", $file) or die "could not open $file, $!\n";
}
How about put all your fp in an array and open them separately?
 
T

Tad J McClellan

ela said:
While I succeeded in producing generic file names, I found that I cannot
name the file pointer


Using the proper terminology can go a long way toward finding a
solution, because you have a useful search term to use.

It is not a "file pointer", it is a "filehandle".

($PROBLEMFP in the following codes) generically. I
have to keep unknown number of file pointers (depending on the number of
lines in modellist, not known beforehand) open and therefore I have to
create "n" $PROBLEMFP's. Is it possible to achieve this in Perl?


perldoc -q filehandle

How can I make a filehandle local to a subroutine? How
do I pass filehandles between subroutines? How do I
make an array of filehandles?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#!/usr/bin/perl
my ( $iteration, $modellist ) = @ARGV;


You should enable warnings and strictures.

use warnings;
use strict;

open( my $FPM, '<', "$modellist") or die "could not open '$modellist' $!";
^ ^
^ ^

perldoc -q vars

What's wrong with always quoting "$vars"?

$lineM = <$FPM>;

chomp $lineM;
@models = split(/\t/, $lineM);
foreach (@models) {


You do not need the @models temporary variable:

foreach ( split(/\t/, $lineM) ) {
 
E

Eric Pozharski

While I succeeded in producing generic file names, I found that I cannot
name the file pointer ($PROBLEMFP in the following codes) generically. I
have to keep unknown number of file pointers (depending on the number of
lines in modellist, not known beforehand) open and therefore I have to
create "n" $PROBLEMFP's. Is it possible to achieve this in Perl?



#!/usr/bin/perl

where is yours C said:
my ( $iteration, $modellist ) = @ARGV;

open( my $FPM, '<', "$modellist") or die "could not open '$modellist' $!";

$lineM = <$FPM>;
@models = split(/\t/, $lineM);

Strange, $modellist seems to be one-line of tab-separated filenames?

my %index;
foreach (@models) {
$modelscript = $_ . "_script.txt";
$modellog = $_ . ".log";

$index{$_}{script} = $_ . "_script.txt";
$index{$_}{log} = $_ . ".log";
##########################
open( my $PROBLEMFP, '>', "$modelscript") or die "could not open
'$modelscript' $!";

Note lack of double-quotes around filename

open $index{$_}{fh}, '>', $index{script} or
die "could not open '$index{script}': $!";

And now most fascinating part, you can't

print $index{$_}{fh} "bwahaha\n";

read C<perldoc -f print> for solution. And be careful, number of open
filehandles is limited.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top