Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

writing an array of data to a file

by Anonymous Monk
on Aug 14, 2006 at 15:25 UTC ( [id://567250]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greeting O wise ones

I'm trying to write an array called @rows to an output file $position_output_path, like so:

open(OUTPUTFILE, '>$position_output_path') or die "Could not open +$position_output_path:$!\n"; while (<OUTPUTFILE>) { print OUTPUTFILE @rows;} close(OUTPUTFILE) or die "Could not close $position_output_path:$! +\n";
The array is definitely not empty, and I have definitely given write permission to the output file. However, when I check the contents of the file, there is nothing there.

Could someone tell me what depressingly newbish mistake I'm making?

Kind regards

C J

Replies are listed 'Best First'.
Re: writing an array of data to a file
by davorg (Chancellor) on Aug 14, 2006 at 15:32 UTC
    open(OUTPUTFILE, '>$position_output_path')

    Variables aren't expanded in single-quoted strings. You're opening a file called $position_output_path. You need double quotes.

    while (<OUTPUTFILE>) { print OUTPUTFILE @rows;}

    While you can read from OUTPUTFILE (which is opened for writing only - so you aren't going to get any data back anyway) write a record to the filehandle. That's not right. You probably just need:

    print OUTPUTFILE @rows;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: writing an array of data to a file
by davido (Cardinal) on Aug 14, 2006 at 15:35 UTC

    You're confusing reading of files with writing to files. Use the diamond operator (<OUTPUTFILE>) to read from a file. When you're writing to a file, there's no need to use the diamond operator, and in fact it won't work to use it.

    Here's how to do it:

    open( OUTPUTFILE, '>', $position_output_path ) or die "Couldn't open $position_output_path\n$!"; foreach my $row ( @rows ) { print OUTPUTFILE $row, "\n"; } close( OUTPUTFILE ) or die "Couldn't close $position_output_path\n$!";

    That ought to do the trick!


    Dave

      Thanks, folks! *slaps head*
Re: writing an array of data to a file
by derby (Abbot) on Aug 14, 2006 at 15:31 UTC

    Greetings back O seeker of wisdom.

    Your basic problem is the while loop. Remove it and see what happens:

    open(OUTPUTFILE, '>$position_output_path') or die "Could not open $position_output_path:$!\n"; print OUTPUTFILE @rows; close(OUTPUTFILE) or die "Could not close $position_output_path:$!
    open(OUTPUTFILE, ">$position_output_path") or die "Could not open $position_output_path:$!\n"; print OUTPUTFILE @rows; close(OUTPUTFILE) or die "Could not close $position_output_path:$!

    -derby

    Update: Need double quotes as davorg points out below.

      That still opens and writes to a file called "$position_output_path" - and I doubt that's what the original poster wants :-)

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: writing an array of data to a file
by McDarren (Abbot) on Aug 14, 2006 at 15:33 UTC
    heh... your while loop is un-necessary and wrong in this case. You would generally only have a loop like that if you were reading from a file. All you need in this case is something like:
    for (@rows) { print OUTPUTFILE "$_\n"; }
    (assuming that you want a newline after each element of @rows)

    Cheers,
    Darren :)

Re: writing an array of data to a file
by GrandFather (Saint) on Aug 14, 2006 at 21:12 UTC

    Another couple of tips:

    1/ it is generally considered good practice to use the three parameter open:

    open (OUTPUTFILE, '>', $position_output_path) or die "Could not open . +..";

    2/ print @array prints the stringified contents of an array without any additional whitespace. print "@array" prints the stringified contents of an array with $, added between each item.

    Note also that join can be used to inster whatever seperators you like between elements of an array.

    Consider:

    my $var; my @array = ('a', 1.1, \$var); print @array; print "\n@array"; print "\n", join "\n", @array;

    Prints:

    a1.1SCALAR(0x182f68c) a 1.1 SCALAR(0x182f68c) a 1.1 SCALAR(0x182f68c)

    DWIM is Perl's answer to Gödel
Re: writing an array of data to a file
by mickeyn (Priest) on Aug 14, 2006 at 17:33 UTC
    try:
    use Tie::File; tie my @file, 'Tie::File', $position_output_path; @file = @rows; untie @file;
    Enjoy,
    Mickey

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567250]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found