Forum Moderators: coopster & phranque

Message Too Old, No Replies

Looping through the array

         

mdharrold

4:07 am on May 24, 2001 (gmt 0)

10+ Year Member



I know this should be easy and I have tried everything else I can find, but I am opening a file containing a simple 5 element array seperated by the default "\n". When I use the array elements, they get jumbled.

What am I doing wrong this time?

sugarkane

9:00 am on May 24, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mdh,

What code are you using?

mdharrold

9:45 pm on May 24, 2001 (gmt 0)

10+ Year Member



I'm using Perl. I realized today that I was looping through the array when reading and not when writing.

Is there a "best" way to do an array loop?

sugarkane

10:16 pm on May 24, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the usual way of reading a file into an array and doing something with it:

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];

mdharrold

10:28 pm on May 24, 2001 (gmt 0)

10+ Year Member



I have seen that before and had it work. The one thing I have tried to do with no luck is open a file, read it into an array, and then use each seperate element where I want it in the output.

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.

theperlyking

11:03 pm on May 24, 2001 (gmt 0)

10+ Year Member



Could you post the snippet of code that appears to be jumbling the array?

mdharrold

12:06 am on May 25, 2001 (gmt 0)

10+ Year Member



This is the code that writes to the file.

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;

mdharrold

12:35 am on May 25, 2001 (gmt 0)

10+ Year Member



Sorry about the smiley faces above.

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;

sugarkane

8:01 am on May 25, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like your problem is the brackets in your foreach loop:

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 :-)

mdharrold

11:05 pm on May 25, 2001 (gmt 0)

10+ Year Member



That didn't fix it either.
This is how it looks now:

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?

sugarkane

11:24 am on May 26, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have to admit, md, that I'm having trouble seeing what you're trying to do here. There are a couple of things to try:

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.

theperlyking

3:17 pm on May 26, 2001 (gmt 0)

10+ Year Member



So does it still jumble up the array? Could you post what the array looks like before and after?

I have to admit last time I did something like this I used a hash as it worked out easier..

mdharrold

11:01 pm on May 27, 2001 (gmt 0)

10+ Year Member



Before the script runs, the array looks like this:
1
1
1
1
1

After it runs:
2 1
1
1
1

I am just using this as a vote counter for a pretty simple survey.