open (FP, "path/to/file");
@array=<FP>;
close(FP);
foreach $i (@array) {
# do something with $i
# eg print "$i<br>";
}
With the 'foreach' command, the variable $i 'becomes' the next element of the array in every loop of the code between the { } braces, so you can use it to do whatever you want with each array element.
For instance: if you had an array with three elements,
foreach $i (@array) {
print $i;
}
would produce the same results as
print $array[0];
print $array[1];
print $array[2];
For example:
<i>Using a table and calling a different element into each cell.</i>
They always work the first time. But each time I change the value of an element, the array begins to resemble more of a mess than anything else.
My guess is that I am writing into the file wrong. But I have never read anything saying that you have to do something special to keep the array file in the format you want.
open (A, "< $path/$A");
@A=<A>;
close A;
foreach $A(@A) {
$Value=$key
}
if ($FORM{Q2A} eq "1")
{$A[0]++;}
elsif ($FORM {Q2A} eq "2")
{$A[1]++;}
elsif ($FORM{Q2A} eq "3")
{$A[2]++;}
elsif ($FORM{Q2A} eq "4")
{$A[3]++;}
else
{$A[4]++;}
open (A, "> $path/$A");
print A "@A";
close A;
open (A, "< $path/$A");
@A=<A>;
close A;
foreach $A(@A) {
$Value=$key
}
if ($FORM{Q2A} eq "1")
{$A[0]++;}
elsif ($FORM {Q2A} eq "2")
{$A[1]++;}
elsif ($FORM{Q2A} eq "3")
{$A[2]++;}
elsif ($FORM{Q2A} eq "4")
{$A[3]++;}
else
{$A[4]++;}
open (A, "> $path/$A");
print A "@A";
close A;
foreach $A(@A) {
$Value=$key
} <----------------
if ($FORM{Q2A} eq "1")
{$A[0]++;}
etc
Your foreach loop doesn't actually do anything with the $A variable - you should move the second bracket to the end of the if / elsif / else section.
BTW - you can fix the smiley face problem by clicking on 'owner edit' next to your post and turning them off :-)
open (A, "< $path/$a);
@A=<A>;
close A;
foreach $A(@A) {
$Value=$A
if ($FORM{Q3A} eq "excellent") {
$A[0]++;
}
elsif ($FORM {Q3A} eq "good") {
$A[1]++;
}
elsif ($FORM{Q3A} eq "fair") {
$A[2]++;
}
elsif ($FORM{Q3A} eq "poor") {
$A[3]++;
}
else {
$A[4]++;
}
}
open (A, "> $path/$a);
print A "@A";
close A;
That is what you meant isn't it?
The line $Value=$A should end in a semicolon - but as you don't use $Value again in your loop, you should perhaps delete it anyway.
To write the array back to a file, use:
open(A, "> $path/$A");
foreach $i (@) { print A "$i\n";}
close A;
If neither of these fix it, let me know what you're trying to do and we might be able to go at it from a different direction.