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.
| [reply] |
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. | [reply] |
Re: Inserting values into an array
by tlm (Prior) on Jun 01, 2005 at 14:00 UTC
|
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!
| [reply] [d/l] [select] |
|
|
The OP is actually setting $/ correctly: $/="" reads paragraphs, as the post implies (s)he wants.
| [reply] |
|
|
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?
| [reply] |
|
|
|
|
|
|
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 = <>;
| [reply] [d/l] [select] |
Re: Inserting values into an array
by Joost (Canon) on Jun 01, 2005 at 14:02 UTC
|
If you want to add each paragraph to the array, you'll want push or unshift:
while (<>) {
push @data,$_; # adds $_ to the end of @data
}
If you want to replace the whole array with the single paragrah, you can do:
while (<>) {
@data = $_;
}
You would probably also benefit from reading perldata.
| [reply] [d/l] [select] |
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.
| [reply] |
Re: Inserting values into an array
by bart (Canon) on Jun 01, 2005 at 14:04 UTC
|
my @array = <>;
will do.
| [reply] [d/l] |
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";
}
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
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";
| [reply] [d/l] |
|
|