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

hi how i can save the data in an array within a loop i read data from a file and i want to store it in an array one paragraph of the data as one element i wrote
my @data; $/=""; while(<>) { $data=$_; print "@data\n"; }
but its giving me error plz help

Retitled by davido from 'array'.

Replies are listed 'Best First'.
Re: Inserting values into an array
by Corion (Patriarch) on Jun 01, 2005 at 14:01 UTC

    Reports of the great interpunction shortage have been overexaggerated. You no longer need to save precious interpunction characters in your prose text to have more of them for your Perl code.

    Honestly, if you post your question as one run-on sentence, you are asking a lot of the people you want an answer from.

    If you want to load, modify and then save data again, Tie::File is your friend - it makes a file appear as an array, and an array appear as a file.

    If you want your code to Just Work, you will have to put in some more effort, like telling us what the error is, and what you expected to happen. Maybe How (not) to ask a question can help you there.

Re: Inserting values into an array
by monarch (Priest) on Jun 01, 2005 at 14:05 UTC
    I just know I'm going to be modded down but I have to express what I think about this anyway.

    I don't know of ANY language where sentences or concepts are not broken down. In Japanese there is the tiny circle, indicating end of sentence. In English we have the period or "full stop".

    Regardless of how well an anonymous person knows English they should be able to logically contain parts of their question into a sentence.

    I like to give a lot of lattitude to those poor in English because unlike them I myself have not gone to the trouble of learning another language well. Thus I always like to help out where I can. I'm sorry but in this case, I just cannot, because without breaking up the sentences it is extremely difficult to understand.

Re: Inserting values into an array
by tlm (Prior) on Jun 01, 2005 at 14:00 UTC

    I think you want

    my @data; $/=""; while(<>) { push @data, $_; }
    or simply
    my @data = <>;
    Don't set $/="": this effectively turns off linewise input, and you end up slurping the entire input from the file as one long string.

    Update: Edited in response to Joost's and Limbic~Region's comment. Once again I am humbled by how much Perl I still have to learn!

    the lowliest monk

        Sorry, I did have difficulty parsing the OP, but for paragraphs I would think that setting $/="\n\n", or something like that, would be more appropriate, no?

        the lowliest monk

Re: Inserting values into an array
by thundergnat (Deacon) on Jun 01, 2005 at 14:04 UTC
    my @data; $/ = ''; while (<>){ push @data, $_; }

    Or more succintly:

    $/ = ''; my @data = <>;
Re: Inserting values into an array
by Joost (Canon) on Jun 01, 2005 at 14:02 UTC
Re: Inserting values into an array
by revdiablo (Prior) on Jun 01, 2005 at 17:08 UTC

    Taking a step back, it seems like there are a couple of very fundamental pieces of knowledge you are missing. This place is certainly a great way to learn a lot about Perl, but sometimes it's nice to have a good foundation laid before trying to do (and ask for help with) complex tasks. Even though your question appears to have been answered a few times over, you might want to read through Beginning Perl. It is a free book about Perl, and it's quite good. It could help you get a nice jump-start in your journey.

Re: Inserting values into an array
by bart (Canon) on Jun 01, 2005 at 14:04 UTC
Re: Inserting values into an array
by prasadbabu (Prior) on Jun 01, 2005 at 14:01 UTC

    Question is not clear.

    With the assumption, i suggest push().

    my @data; $/=""; while(<>) { $data=$_; push (@data, $data); print "@data\n"; }

    Prasad

      That's great . . . if you want to copy each paragraph into a useless variable (that won't pass use strict, BTW) and print the entire accumulated contents so far for each paragraph you read.

      --
      We're looking for people in ATL

        Fletch, i know that it is useless variable, i just changed his coding by just including push statement. The OP may use that useless variable later in useful way, which we don't know.

        my @data; $/ = ''; while (<>){ push @data, $_; } print "@data";

        Prasad