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

This is really a two part question, but I'll stick to the first one until it's resolved.

Basically I have a while loop from a sql query. I'm wanting to print the contents into a new file with the correct format as follows:

while (@data = $query->fetchrow) { open (NEWDATA, ">>newdata.txt") or die "Can't append to $!\n"; $~ = PB_BODY_LIMITED; write; close (NEWDATA); }


In case you're interested, I'll be following this question with how to write a sub routine for any while loop. You can post your thoughts, but let me try it first.

Thanks!

Replies are listed 'Best First'.
Re: File/Fetch Array
by jeroenes (Priest) on Apr 04, 2001 at 01:20 UTC
    You can move your open and $~ statements out the while loop. Saves you execution time. It is more clear to name the filehandle and the format identical:
    open NEWDATA, ">>newdata.txt" or die "Can't append to newdata.txt: $!" +; format NEWDATA .....#stuff goes here . write NEWDATA while( @data = $query->fetchrow ); close NEWDATA;
    See perlform for more about formats. (The formats are a case apart. Different namespace and so on.)

    Hope this helps,

    Jeroen
    "We are not alone"(FZ)

      Yup, that worked. Worked too well actually. With a little extra effort I managed to get exactly what I wanted. Thanks!
Re: File/Fetch Array
by suaveant (Parson) on Apr 04, 2001 at 00:29 UTC
    If you want to write to the filehandle instead of STDOUT I beleieve you need to do
    write NEWDATA;
                    - Ant
      Ant- thanks for your comment, unfortunately that doesn't work as I've tried it many times in various ways :(. Any other suggestions?
        Umm? Did you end your format with a '.'?

        I usually just use sprintf, myself, tho I suppose format is the better way.
                        - Ant