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

I need to read data from an array and write into a text file

Sorry guys, I know this must be quite simple for you guys, but I am new to Perl, and stuck at that point. I would appreciate for any help!...

Thanks
AA

20050316 Edit by ysth: p tags, copy title into body per consideration

  • Comment on I need to read data from an array and write into a text file

Replies are listed 'Best First'.
Re: I need to read data from an array and write into a text file
by JediWizard (Deacon) on Mar 11, 2005 at 19:36 UTC

    See open for info on opening a file.

    my @array = qw(some data you want to print to a file); open(my $fh, '>', '/path/to/a/text/file.txt') || die "could not open o +utput file : $!\n"; print $fh @array; close($fh);

    That will wirte the array to the file. If you'd like to append data to the end of the file rather than over writing it:

    open(my $fh, '>>', '/path/to/a/text/file.txt') || die "could not open +output file : $!\n";

    Hope that helps


    A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

    —His Holiness, The Dalai Lama

      Thanks your help, that worked great... The only thing I need to do is to add a NewLine for every data. I couldn't handle with that print statement. Thanks your help again.

        This will add the new lines between each element of the array.

        my @array = qw(some data you want to print to a file); open(my $fh, '>', '/path/to/a/text/file.txt') || die "could not open o +utput file : $!\n"; print $fh join("\n", @array); close($fh);

        See join

        Update: you could also do this:

        my @array = qw(some data you want to print to a file); open(my $fh, '>', '/path/to/a/text/file.txt') || die "could not open o +utput file : $!\n"; { local $, = "\n"; print $fh @array; } close($fh);

        See: perlvar. Sorry I had the wrong variable there originally... hence the update.


        A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

        —His Holiness, The Dalai Lama

Re: I need to read data from an array and write into a text file
by jZed (Prior) on Mar 11, 2005 at 19:43 UTC
    Do you want to write the array into the file in a manner that allows you to then retrieve the data back out of the file? If so, you'll need to decide on an easily parseable format for the file such as CSV (Comma-separated Values, Tab "Delimited", etc.), XML, Fixed Width, etc.

    There are modules on CPAN that will take an array of values and turn it into each of those formats. If you let us know what your goal is, we can provide suggestions.

Re: I need to read data from an array and write into a text file
by bart (Canon) on Mar 11, 2005 at 19:48 UTC
    Please do not put your question in your title and rubbish in your post body.
Re: I need to read data from an array and write into a text file
by moggs (Sexton) on Mar 12, 2005 at 12:14 UTC
    Hi

    This might help:

    It's actually possible to manage the text file as you would an array, as long as you've got the right CPAN stuff installed.

    A full explaination is on page 500 of the PERL Cookbook or you can read it at http://www.india-seo.com/perl/cookbook/ch14_08.htm

    I've used this quite extensively - really quite good in my opinion.

    Moggs