foreach loop test

R

Robin

foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or middle
of the array without having to use a while loop....any suggestions?
Thanks.
 
M

Matija Papec

X-Ftn-To: Robin

Robin said:
foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or middle
of the array without having to use a while loop....any suggestions?

for my $i (0 .. $#test) {
my $element = $test[$i];
print "$i. element is $element\n";
...
}

p.s how would while loop show array index?
 
S

Steven Kuo

foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or middle
of the array without having to use a while loop....any suggestions?
Thanks.



One way would be to use references:


my @foo = ( 99, 86, 7, 99, 007 );

my $first = \$foo[0];
my $last = \$foo[-1];

for (@foo) {
if (\$_ == $first) {
print "First. ";
} elsif (\$_ == $last) {
print "Last. ";
}

print $_, "\n";
}


See 'perldoc perlref' for details.
 
G

Gregory Toomey

Robin said:
foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or middle
of the array without having to use a while loop....any suggestions?
Thanks.

I asked this 3 years ago. The "best" solution was along the following lines:

for (@a=((1) x 4)) {

print \$_, " $_\n" ;

print "first $_\n" if \$_ == \$a[0] ;
print "last $_\n" if \$_ == \$a[$#a] ;
}


SCALAR(0x80ca55c) 1
first 1
SCALAR(0x80d0bb4) 1
SCALAR(0x80d0ca4) 1
SCALAR(0x80d0cf8) 1
last 1


gtoomey
 
T

Tore Aursand

[...]
p.s how would while loop show array index?

He may be thinking of reading data from handles, where one can access $.
to find out which element one is at;

while ( <DATA> ) {
print "$. => $_";
}
 
J

Jeff 'japhy' Pinyan

One way would be to use references:

my @foo = ( 99, 86, 7, 99, 007 );

This method is fine -- I use it too -- IF YOUR DATA does not have two
references to the same data:

my @foo = (\$a, \$b, \$c, \$b);

will cause this method to break.
 
T

Ted Zlatanov

foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or
middle of the array without having to use a while loop....any
suggestions?

To me, this question 95% of the time indicates a suboptimal approach
to the problem.

You can use the index, as Matija Papec showed.

You can also use pop() or shift() to shrink the array dynamically, and
see what's left in it to see where you are.

You can take the first and last elements off beforehand (with shift()
and pop()) and then process the middle normally.

If, however, you show a more complete example of what you're trying
to do, you may get a better solution that will help you improve your
Perl. Your example, as it is, does not need to know where in the
array it's working.

Ted
 
C

christie

Guys,

Try this,

$count = @test;

for($i=0; $i<$count;$i++){
$tmp = $test[$i];
if($i == 0){print "First array: $tmp\n";}
if($i == floor($count/2)){print "Middle array: $tmp\n";}
if($i== $count){print "Last array: $tmp\n";}
}


Cheers,
 
B

Brad Baxter

Try this,

$count = @test;

for($i=0; $i<$count;$i++){
$tmp = $test[$i];
if($i == 0){print "First array: $tmp\n";}
if($i == floor($count/2)){print "Middle array: $tmp\n";}
if($i== $count){print "Last array: $tmp\n";}
}

Did you try it? It will never print "Last array ..."

use warnings;
use strict;
use POSIX;

my @test = ( 1, 2, 3, 4 );
my $count = $#test;

for my $i ( 0 .. $count ) {
my $tmp = $test[$i];
if ($i == 0) {
print "First array: $tmp\n"; }
if ($i == floor($count/2)) {
print "Middle array: $tmp\n"; }
if ($i == $count) {
print "Last array: $tmp\n"; }
}

__END__
First array: 1
Middle array: 2
Last array: 4

Though the definition of "Middle" is muddled. :)

Regards,

Brad
 
C

christie

Brad Baxter said:
Try this,

$count = @test;

for($i=0; $i<$count;$i++){
$tmp = $test[$i];
if($i == 0){print "First array: $tmp\n";}
if($i == floor($count/2)){print "Middle array: $tmp\n";}
if($i== $count){print "Last array: $tmp\n";}
}

Did you try it? It will never print "Last array ..."

Of course, it will never print the last array 'cause $i<$count. It
should be $i<=$count. An entry level scripter should be able to fix
this bug.

use warnings;
use strict;
use POSIX;

my @test = ( 1, 2, 3, 4 );
my $count = $#test;

for my $i ( 0 .. $count ) {
my $tmp = $test[$i];
if ($i == 0) {
print "First array: $tmp\n"; }
if ($i == floor($count/2)) {
print "Middle array: $tmp\n"; }
if ($i == $count) {
print "Last array: $tmp\n"; }
}

__END__
First array: 1
Middle array: 2
Last array: 4

Though the definition of "Middle" is muddled. :)

Regards,

Brad
 
B

Brad Baxter

Brad Baxter said:
Try this,

$count = @test;

for($i=0; $i<$count;$i++){
$tmp = $test[$i];
if($i == 0){print "First array: $tmp\n";}
if($i == floor($count/2)){print "Middle array: $tmp\n";}
if($i== $count){print "Last array: $tmp\n";}
}

Did you try it? It will never print "Last array ..."

Of course, it will never print the last array 'cause $i<$count. It
should be $i<=$count. An entry level scripter should be able to fix
this bug.


You're welcome. :)

Brad
 
R

Robin

Robin said:
foreach (@test)
{
print;
}

for this code, I'd like some way to test if $_ is the last first or middle
of the array without having to use a while loop....any suggestions?
Thanks.

thanks for all the info, I'll use the for loop...
-Robin
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top