generic variable name possible?

Discussion in 'Perl Misc' started by ela, Apr 18, 2009.

  1. ela

    ela Guest

    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' $!";
    }
     
    ela, Apr 18, 2009
    #1
    1. Advertising

  2. ela

    kun niu Guest

    On 4ÔÂ19ÈÕ, ÉÏÎç12ʱ17·Ö, "ela" <> wrote:
    > 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?
     
    kun niu, Apr 18, 2009
    #2
    1. Advertising

  3. ela <> wrote:
    >
    > 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) ) {


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Apr 18, 2009
    #3
  4. On 2009-04-18, ela <> wrote:
    >
    > 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<use strict;> and C<use warnings;>?

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

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, Apr 18, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,071
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    448
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,415
    Joe Smith
    Apr 22, 2007
  4. mfglinux
    Replies:
    11
    Views:
    723
    Roberto Bonvallet
    Sep 12, 2007
  5. David Filmer
    Replies:
    19
    Views:
    256
    Kevin Collins
    May 21, 2004
Loading...

Share This Page