How to iterator array in order?

P

Peng Yu

Hi,

I know at least three ways to iterator an array. But I'm not sure if
the first two iteration methods would iterator the array in order
(from the first element to the last element). Can somebody let me
know?

What is the best way to iterator an array?

Thanks,
Peng

#!/usr/bin/perl

@array = (1, 2, 3, 4);

foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}
 
S

sln

Hi,

I know at least three ways to iterator an array. But I'm not sure if
the first two iteration methods would iterator the array in order
(from the first element to the last element). Can somebody let me
know?

What is the best way to iterator an array?

Thanks,
Peng

#!/usr/bin/perl

@array = (1, 2, 3, 4);

foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}

Yes, all three would. The 'foreach' is a special,
unrelated case. The target becomes an alias for the
actual element. If you write to the target, you write
to the element, be careful on that one.


sln
 
J

Jürgen Exner

Peng Yu said:
I know at least three ways to iterator an array. But I'm not sure if
the first two iteration methods would iterator the array in order
(from the first element to the last element). Can somebody let me
know?
What is the best way to iterator an array?
@array = (1, 2, 3, 4);

foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

That's exactly the same as the first version except that you are using a
different variable ($_ versus $elem). "for" and "foreach" are synonyms.
Which one is better depends only on if you want to use $_ (often as the
default in following operations) or not.
for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}

This does something totally different because it iterates over the
indices of the array.
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with 0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.

As for your original question: use whatever is is more appropriate for
the task at hand: if you want to iterate over the elements then do so.
If you want to iterate over the indices (sometimes that cannot be
avoided) then iterate over the indices.

jue
 
U

Uri Guttman

foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}

s> Yes, all three would. The 'foreach' is a special,
s> unrelated case. The target becomes an alias for the
s> actual element. If you write to the target, you write
s> to the element, be careful on that one.

huh? for and foreach are EXACTLY the same in perl - they are aliases for
the same syntax spot. the type of loop is controlled by the presence of
; inside the (). you have either a list loop (the first two examples) or
a c style loop (the last example).

the first two examples are the same except for using $_ vs a
lexical. the 'for' vs 'foreach' there is irrelevent (try it). both alias
each element to the loop variable.

uri
 
P

Peter J. Holzer

for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}

This does something totally different because it iterates over the
indices of the array.
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with 0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.

Frankly, in my world $[ doesn't exist. Anyone who sets it outside of an
obfu, should be drawn and quartered. I won't clutter up my code just
because some idiot might set it. So if I need to iterate over the
indices of an array I use:

for my $i (0 .. $#array) { ... }

hp
 
J

Jürgen Exner

Peter J. Holzer said:
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with 0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.

Frankly, in my world $[ doesn't exist. Anyone who sets it outside of an
obfu, should be drawn and quartered.

Agreed :)
I won't clutter up my code just
because some idiot might set it. So if I need to iterate over the
indices of an array I use:

for my $i (0 .. $#array) { ... }

Why not
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

jue
 
S

sln

"s" == sln <[email protected]> writes:
foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

for($i = 0; $i <= $#array; ++ $i) {
print "$array[$i]\n";
}

s> Yes, all three would. The 'foreach' is a special,
s> unrelated case. The target becomes an alias for the
s> actual element. If you write to the target, you write
s> to the element, be careful on that one.

huh? for and foreach are EXACTLY the same in perl - they are aliases for
the same syntax spot. the type of loop is controlled by the presence of
; inside the (). you have either a list loop (the first two examples) or
a c style loop (the last example).

the first two examples are the same except for using $_ vs a
lexical. the 'for' vs 'foreach' there is irrelevent (try it). both alias
each element to the loop variable.

uri

Really good to know. I will try it, I will put it on the queue of 10,000
other things to try. Until I do, I must take your word for it and be sure
to not asign to $_ unless inside a while() loop. Sound good?

$@_,$_,$_[n] still got me cornfused.

sln
 
S

sln

Hi,

I know at least three ways to iterator an array. But I'm not sure if
the first two iteration methods would iterator the array in order
(from the first element to the last element). Can somebody let me
know?

What is the best way to iterator an array?

Thanks,
Peng

#!/usr/bin/perl

@array = (1, 2, 3, 4);

foreach(@array) {
print "$_\n";
}

for my $elem (@array) {
print "$elem\n";
}

for($i = 0; $i <= $#array; ++ $i) {
^^
Don't do this here, it just looks bad.
As a C++ programmer, you know the problems with
compiler optimizations on pre-increment.

In Perl its ok, no such thing as optimizations.
$i = ++$i is ok.

sln
 
J

John W. Krahn

Jürgen Exner said:
Peter J. Holzer said:
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.
Frankly, in my world $[ doesn't exist. Anyone who sets it outside of an
obfu, should be drawn and quartered.

Agreed :)
I won't clutter up my code just
because some idiot might set it. So if I need to iterate over the
indices of an array I use:

for my $i (0 .. $#array) { ... }

Why not

Why not indeed!
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
 
S

sln

Jürgen Exner said:
Peter J. Holzer said:
On 2008-11-21 05:30, Jürgen Exner <[email protected]> wrote:
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with 0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.
Frankly, in my world $[ doesn't exist. Anyone who sets it outside of an
obfu, should be drawn and quartered.

Agreed :)
I won't clutter up my code just
because some idiot might set it. So if I need to iterate over the
indices of an array I use:

for my $i (0 .. $#array) { ... }

Why not

Why not indeed!
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
Errors?
asdf:>> for my $i (0 .. @array+1)

Give an example please.

sln
 
M

Martien Verbruggen

On Sun, 23 Nov 2008 20:06:15 GMT,
Jürgen Exner said:
BTW: if you are using $#array instead of the length of the array then
you probably should also start your iteration with $[ instead of with 0.
The combination you are using doesn't make much sense because your start
value is hardcoded to ignores non-default values for $[ while your end
respects them.
Frankly, in my world $[ doesn't exist. Anyone who sets it outside of an
obfu, should be drawn and quartered.

Agreed :)

I won't clutter up my code just
because some idiot might set it. So if I need to iterate over the
indices of an array I use:

for my $i (0 .. $#array) { ... }

Why not

Why not indeed!
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
Errors?
asdf:>> for my $i (0 .. @array+1)

$ perl -wl
my @foo = (1, 2, 3);
print "\$#foo and \@foo + 1 are ",
($#foo == @foo + 1) ? "" : "not ",
"the same";
__END__

Martien
 
S

sln

On Sun, 23 Nov 2008 11:31:32 -0800, "John W. Krahn"
[snip]
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
Errors?
asdf:>> for my $i (0 .. @array+1)

$ perl -wl
my @foo = (1, 2, 3);
print "\$#foo and \@foo + 1 are ",
($#foo == @foo + 1) ? "" : "not ",
"the same";
__END__

Martien

Who said $#foo and \@foo + 1 are the same amount??
for my $i (0 .. @array+1)...

'@array+1' is just a number. Any other errors?

sln
 
M

Martien Verbruggen

On Sun, 23 Nov 2008 20:38:49 GMT,
On Sun, 23 Nov 2008 11:31:32 -0800, "John W. Krahn"
[snip]
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
Errors?
asdf:>> for my $i (0 .. @array+1)

$ perl -wl
my @foo = (1, 2, 3);
print "\$#foo and \@foo + 1 are ",
($#foo == @foo + 1) ? "" : "not ",
"the same";
__END__

Martien

Who said $#foo and \@foo + 1 are the same amount??

Jürgen Exner did, implicitly. He wrote the first bit of code above (you
snipped the attribution, but left that code), in response to someone
else writing a loop with $#array, in code that I quoted in the previous
message, but that you snipped.
for my $i (0 .. @array+1)...

'@array+1' is just a number. Any other errors?

But it's the wrong number for this context. Not all numbers are
equivalent in all contexts.

Even without the original code, @array + 1 is an index that is too high
by two for the size of the array it's supposed to be indexing.

I don't see any other errors, but I think this error on its own is
enough to be commented on.

Martien
 
S

sln

31:32 -0800, "John W. Krahn"
[snip]
for my $i (0 .. @array+1)
Doesn't seem to introduce much clutter to me...

Just errors. :(


John
Errors?
asdf:>> for my $i (0 .. @array+1)

$ perl -wl
my @foo = (1, 2, 3);
print "\$#foo and \@foo + 1 are ",
($#foo == @foo + 1) ? "" : "not ",
"the same";
__END__

Martien

Who said $#foo and \@foo + 1 are the same amount??

Jürgen Exner did, implicitly. He wrote the first bit of code above (you
snipped the attribution, but left that code), in response to someone
else writing a loop with $#array, in code that I quoted in the previous
message, but that you snipped.
for my $i (0 .. @array+1)...

'@array+1' is just a number. Any other errors?

But it's the wrong number for this context. Not all numbers are
equivalent in all contexts.

Even without the original code, @array + 1 is an index that is too high
by two for the size of the array it's supposed to be indexing.

I don't see any other errors, but I think this error on its own is
enough to be commented on.

Martien

There was no context mentioned. I got no problem with
the range. In fact, in my opinion,
for my $i (0 .. @array+65535)
is way better than
for my $i (0 .. $#array)

it clearly states the truth about the size of arrays !!
Have a nice day!


sln
 
M

Martien Verbruggen

On Sun, 23 Nov 2008 23:26:57 GMT,
There was no context mentioned. I got no problem with

There was. Please check the thread.
the range. In fact, in my opinion,
for my $i (0 .. @array+65535)
is way better than
for my $i (0 .. $#array)

it clearly states the truth about the size of arrays !!

That is just plain silly.

Martien
 
J

Jürgen Exner

John W. Krahn said:
Why not indeed!


Just errors. :(

Well, yeah, fine, whatever.

s/+/-/

I guess it was pretty obvious what I meant. If you want to make a big
deal out of that typo, then be my guest.

jue
 
C

Charlton Wilbur

J> I guess it was pretty obvious what I meant. If you want to make a
J> big deal out of that typo, then be my guest.

But the fact that an experienced, knowledgeable Perl programmer can so
easily typo that is a sign that it's a dangerous construct to use.

Charlton
 
T

Ted Zlatanov

JE> Well, yeah, fine, whatever.

JE> s/+/-/

JE> I guess it was pretty obvious what I meant. If you want to make a big
JE> deal out of that typo, then be my guest.

The typo's existence is relevant, though: if you, an experienced
programmer, made it, surely beginners will do it too. IMO array offsets
should be a last resort, and very few algorithms need them. I usually
get by with iterating through the list elements, which generates cleaner
code in the vast majority of the cases (and sometimes even improves the
algorithm because it forces me to think differently about it).

Array offsets and memory management are the naughty bits of
programming. They should be covered up in public whenever possible :)

Ted
 
J

Jürgen Exner

Ted Zlatanov said:
IMO array offsets
should be a last resort, and very few algorithms need them. I usually
get by with iterating through the list elements, which generates cleaner
code in the vast majority of the cases (and sometimes even improves the
algorithm because it forces me to think differently about it).

I very strongly agree.
However for that instance the question was what to use if you do need
the indices after all, e.g. to loop through multiple lists in sync or
access multiple elements in each iteration.

jue
 
T

Ted Zlatanov

JE> I very strongly agree.
JE> However for that instance the question was what to use if you do need
JE> the indices after all, e.g. to loop through multiple lists in sync or
JE> access multiple elements in each iteration.

I know, I just couldn't resist a chance to say "naughty bits" in a
technical context ;)

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,100
Members
47,695
Latest member
KayleneBee

Latest Threads

Top