Creating a file to print to

Discussion in 'Perl Misc' started by Robert, Apr 9, 2007.

  1. Robert

    Robert Guest


    I am trying to create different files to print data to.
    The problem I am having is with the name of the file.

    I have created a loop to create the files, which works fine,
    however later in the program I need to write similar data to the file.

    This is what I am doing but it does not work:

    while ($group <=12){

    print MY.$group.OUTFILE $header_top;
    print MY.$group.OUTFILE "$group";
    print MY.$group.OUTFILE "$header_bottom;


    How can I get the "reference to the file" to change name?

    At the moment I am forced to type:

    $group = "12";
    print MY12OUTFILE $header_top;
    print MY12OUTFILE "$group";
    print MY12OUTFILE $header_bottom;

    $group = "11";
    print MY11OUTFILE $header_top;

    Any suggestions?
    Robert, Apr 9, 2007
    1. Advertisements

  2. Robert

    Mumia W. Guest


    use IO::File;

    my %groups = (
    11 => IO::File->new(...) || die(...),
    12 => IO::File->new(...) || die(...),

    my $group = 1;

    while ($group <= 12) {
    next unless $groups{$group};
    print { $groups{$group} } $header_top;
    print { $groups{$group} } $group;
    print { $groups{$group} } $header_bottom;

    # Close the IO::File objects in %groups.


    Read the doc for "print" again: "perldoc -f print"
    Mumia W., Apr 9, 2007
    1. Advertisements

  3. Not necessary. open works just fine since at least 5.8.

    Why are you using a hash if it is indexed by small, consecutive

    Peter J. Holzer, Apr 14, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.