Position in an array

B

bernd

Hello folks,

just for confirmation: Is my assumption correct that there is no
"implicit" method to know the current index of an array (means: the
index of the item which is stored in $_ in a foreach-loop for example)
without using an "external" counter variable?

In the following I want to traverse the array @testarr and want to
treat the very first item (but it could be any other arbitrary item as
well) specially. Is there a way of "knowing" the index without using
the explicitly defined variable $count?

$count = 0 ;
foreach ( @testarr ) {

if ( $count == 0 ) { print $_ }

# same code for all items ....

$count++ ;
}

So, can I get rid of $count in some way (by using an implicit
mechanism) or do I have to live with this, IMO, long winded way.

Cheers


Bernd
 
R

Robert 'phaylon' Sedlacek

bernd said:
$count = 0 ;
foreach ( @testarr ) {

if ( $count == 0 ) { print $_ }

# same code for all items ....

$count++ ;
}

So, can I get rid of $count in some way (by using an implicit
mechanism) or do I have to live with this, IMO, long winded way.

How about

for my $x (0 .. $#testarr) {
print if $x == 0;
}
 
R

Robert 'phaylon' Sedlacek

Robert said:
for my $x (0 .. $#testarr) {
print if $x == 0;
}

Of course that should be...

for my $x (0 .. $#testarr) {
print $testarr[ $x ] if $x == 0;
}
 
A

anno4000

bernd said:
Hello folks,

just for confirmation: Is my assumption correct that there is no
"implicit" method to know the current index of an array (means: the
index of the item which is stored in $_ in a foreach-loop for example)
without using an "external" counter variable?
Right.


In the following I want to traverse the array @testarr and want to
treat the very first item (but it could be any other arbitrary item as
well) specially. Is there a way of "knowing" the index without using
the explicitly defined variable $count?

$count = 0 ;
foreach ( @testarr ) {

if ( $count == 0 ) { print $_ }

# same code for all items ....

$count++ ;
}

So, can I get rid of $count in some way (by using an implicit
mechanism) or do I have to live with this, IMO, long winded way.

Alternatively you can loop over the index set in the first place:

for my $count ( 0 .. $#testarr ) {
my $el = $testarr[ $count];
print $el unless $count;

# etc.
}

or variants thereof.

Anno
 
T

tfe

Robert 'phaylon' Sedlacek a écrit :
How about

for my $x (0 .. $#testarr) {
print if $x == 0;
}

Might be
for my $x (0 .. $#testarr) {
print $testarr[$x] if $x == 0;
}
or you should do $_ = $test[$x] at the start of the loop...
 
A

anno4000

tfe said:
Robert 'phaylon' Sedlacek a écrit :
How about

for my $x (0 .. $#testarr) {
print if $x == 0;
}

Might be
for my $x (0 .. $#testarr) {
print $testarr[$x] if $x == 0;
}
or you should do $_ = $test[$x] at the start of the loop...

Doing it like that is a very bad idea. $_ is often aliased to
another variable. If it is when this code is called, "$_ = $test[$x]"
will assign the value to an unrelated variable somewhere in the
program, which can be a hard-to-find bug.

Before assigning to $_ you should at least localize the variable:

local $_ = $test[$x];

though a bug in Perl makes even that unsafe in some (rare) situations.
An alternative is another level of aliasing, through a one-shot do
(untested):

for my $x (0 .. $#testarr) {
for ( $testarr[ $x] = {
print unless $x;
# etc.
}
}

Anno
 
B

bernd

@Robert: What You suggested is, basically, what I wanted to avoid (Your
$x is my $count). I think Anno is right: There is no more elegant way
(o.k., this thread made me sure at least ;-).

Thanks to all!

Bernd
 
R

RedGrittyBrick

bernd said:
$count = 0 ;
foreach ( @testarr ) {
if ( $count == 0 ) { print $_ }
# same code for all items ....
$count++ ;
}

Isn't that the same as this?

print $testarr[0];
for (@testarr) {
# ...
}
 
B

bernd

Not at all, I guess. But what is Your intention?

bernd said:
$count = 0 ;
foreach ( @testarr ) {
if ( $count == 0 ) { print $_ }
# same code for all items ....
$count++ ;
}Isn't that the same as this?

print $testarr[0];
for (@testarr) {
# ...



}- Hide quoted text -- Show quoted text -
 
J

J. Gleixner

bernd wrote:

Fixed top-post...
bernd said:
$count = 0 ;
foreach ( @testarr ) {
if ( $count == 0 ) { print $_ }
# same code for all items ....
$count++ ;
}Isn't that the same as this?
print $testarr[0];
for (@testarr) {
Not at all, I guess. But what is Your intention?

I think what the person is showing is, if you only want to print the
first element in @testarr, you can do that by

print $testarr[0];

In other words, there's no need to have a for loop, if all you want to
do is print the first element. I don't think that has anything
to do with your initial question, however based on the code
above that seems to be a possible point to make.
 
R

RedGrittyBrick

bernd top-posted:
Not at all, I guess. But what is Your intention?
I was wondering what yours was.


C:\>perl bernd.pl
--- bernd begins ---
fee
--- bernd ends ---

--- RGB begins ---
fee
--- RGB ends ---



C:\>type bernd.pl
#!perl

@testarr = qw(fee fie foe fum);

print "--- bernd begins ---\n";
#
# bernd wrote
#
$count = 0 ;
foreach ( @testarr ) {
if ( $count == 0 ) { print $_ }
# same code for all items ....
$count++ ;
}
print "\n--- bernd ends ---\n\n";

print "--- RGB begins ---\n";
#
# RGB wrote
# "Isn't that the same as this?"
#
print $testarr[0];
for (@testarr) {
# ...
}
print "\n--- RGB ends ---\n";
 
D

Dr.Ruud

(e-mail address removed)-berlin.de schreef:
$_ is often aliased to
another variable. If it is when this code is called, "$_ = $test[$x]"
will assign the value to an unrelated variable somewhere in the
program, which can be a hard-to-find bug.


"how to solidly spoil the value of $_"
:)
 
M

Mark

bernd said:
Is there a way of "knowing" the index without using
the explicitly defined variable $count?

$count = 0 ;
foreach ( @testarr ) {

if ( $count == 0 ) { print $_ }

# same code for all items ....

$count++ ;
}

So, can I get rid of $count in some way

This following doesn't solve the problem of "'knowing' the index", but
it can detect when the loop is at the desired iteration:

foreach ( @testarr ) {
print if \$_ == \$testarr[0] ;
}
 

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
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top