Re: foreach with array elements problem
by OnionKnight (Sexton) on Oct 22, 2005 at 03:10 UTC
|
I want to use an array element for storing the value of each loop iteration. Just like doing foreach $scalar (1..5) I want to be able to do foreach $array[0] (1..5), is it really that confusing? And obivously this isn't code I wan't to use but example code used to demonstrate the error.
The actual code is quadruple nested foreach loops so I use an array for holding each loop value and then do a join(".", @array); at the end. I could just do my ($iter1, $iter2, $iter3, $iter4); and "$iter1.$iter2.$iter3.$iter4" but right now I'm interested in why this causes an error. | [reply] [d/l] [select] |
|
|
You are out of luck with the syntax you wanted. Syntax is syntax, which you have to strictly follow. Logically, You might have a point to use an array element as the loop variable (as long as it is a scalar, however you must realize the difficulty that this schema introduced, as it is not possible to determine the type of an array element statically, thus it is wise not to introduce what you wanted into a language.), but that's not how the language is defined.
You still can do what you wanted to do, inside each level of your quadruple loop, just push the loop variable into an array.
for my $i (...) {
push @iter, $i;
...
for my $j (...) {
push @iter, $j;
...
for my $k (...) {
push @iter, $k;
...
for my $l (...) {
push @iter, $l;
...
}
}
}
}
On the other hand, I trust that, you realize, the inner loops have access to outter loop variables. The reason to push them into an array is not for gaining access of their values, but rather other convenience that you are seeking. | [reply] [d/l] |
|
|
as long as it is a scalar, however you must realize the difficulty that this schema introduced, as it is not possible to determine the type of an array element statically, thus it is wise not to introduce what you wanted into a language.
As a matter of fact, array elements can only be scalars. perldata is quite clear about this, but what probably leaves no doubt is perldsc:
The most important thing to understand about all data structures in Perl -- including multidimensional arrays--is that even though they might appear otherwise, Perl @ARRAYs and %HASHes are all internally one-dimensional. They can hold only scalar values (meaning a string, number, or a reference). They cannot directly contain other arrays or hashes, but instead contain references to other arrays or hashes.
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] |
|
|
| [reply] |
Re: foreach with array elements problem
by Errto (Vicar) on Oct 22, 2005 at 02:55 UTC
|
perlsyn defines a foreach statement as:
LABEL foreach VAR (LIST) BLOCK
and it seems safe to assume that VAR means a scalar variable (though granted, it does not appear to say this explicitly in perlsyn). | [reply] [d/l] |
Re: foreach with array elements problem
by spiritway (Vicar) on Oct 22, 2005 at 02:56 UTC
|
I don't think it works like that. You're apparently trying to assign all the values to $array[0], which I'm figuring isn't what you had in mind. If you're simply trying to put the index of the array into the corresponding array element, then perhaps you should consider:
foreach $i (1 .. 10) {
$array[$i] = $i;
}
... or words to that effect. | [reply] [d/l] |
|
|
Of course, if that's what you're going for you could achieve that much more easily by saying
@array = (1 .. 10);
| [reply] [d/l] |
|
|
| [reply] |
|
|
"You're apparently trying to "
If you don't have much sense to understand things, it definitely went too far for you to guess things. Don't even think about it, don't do things that are beyong your capability. It is 100% obvious that he never wanted what you thought he wanted. His question was simply and straight: in the foreach syntax, why the var cannot be an array element?
| [reply] |
|
|
I'm sure that if the OP feels I have overstepped my bounds, s/he can speak up and say something. In this case, the words used by the OP, and the code s/he showed, created an ambiguity that could not readily be resolved. I chose one possibility, and responded to it. While my response was awkward insofar as Perl is concerned, and it made an assumption that might be false, it was the first one that actually tried to answer the OP's question. I at least made an effort to help someone, instead of criticizing him or her for real or imagined faults.
Now, tovarishch, I have had enough rudeness from you. If you choose to discuss matters with me in a civilized manner, I will be glad to do so. If you choose to continue to be rude, I won't reply, OK? Have a nice day.
| [reply] |
|
|
| [reply] |
Re: foreach with array elements problem
by monkfan (Curate) on Oct 22, 2005 at 02:56 UTC
|
It's not clear to me what do you want to achieve exactly.
An array slice perhaps?
my @ar_slice = @array[1..234234];
| [reply] [d/l] |
Re: foreach with array elements problem
by revdiablo (Prior) on Oct 22, 2005 at 02:48 UTC
|
What's wrong with using array elements for storing the loop values?
It's a syntax error, that's what's wrong with it.
| [reply] |
|
|
Yeah but what's the correct syntax?
| [reply] |
|
|
To answer the original question, why is it a syntax error?
Going back to perlsyn, here's what it says about the variable in a foreach loop:
If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop.
So even though $array[0] is a scalar value, it cannot exist because there is no local version of @array within the loop. Even if there were a local version of @array within the loop, you would still have to copy those values to the external version of @array from within the loop.
| [reply] |