Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > foreach loop test

Reply
Thread Tools

foreach loop test

 
 
Robin
Guest
Posts: n/a
 
      04-13-2004
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.
--
Regards,
-Robin
--
robin @ infusedlight.net


 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      04-13-2004
X-Ftn-To: Robin

"Robin" <robin @ infusedlight.net> wrote:
>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?



--
Matija
 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      04-13-2004
On Tue, 13 Apr 2004, Robin wrote:

> 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.

--
Hope this helps,
Steven

 
Reply With Quote
 
Gregory Toomey
Guest
Posts: n/a
 
      04-13-2004
Robin wrote:

> 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.
> --
> Regards,
> -Robin
> --
> robin @ infusedlight.net


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(0x80d0cf 1
last 1


gtoomey
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      04-14-2004
On Tue, 13 Apr 2004 23:59:20 +0200, Matija Papec wrote:
>> 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?


> [...]
> 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 "$. => $_";
}


--
Tore Aursand <(E-Mail Removed)>
"I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
upon witnessing the explosion of the first atomic bomb.
 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      04-14-2004
On Tue, 13 Apr 2004, Steven Kuo wrote:

>On Tue, 13 Apr 2004, Robin wrote:
>
>> 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?

>
>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.

--
Jeff Pinyan RPI Acacia Brother #734 2004 IT Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      04-14-2004
On Tue, 13 Apr 2004, robin @ infusedlight.net wrote:

> 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
 
Reply With Quote
 
christie
Guest
Posts: n/a
 
      04-16-2004
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,




Ted Zlatanov <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On Tue, 13 Apr 2004, robin @ infusedlight.net wrote:
>
> > 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

 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      04-17-2004
On Fri, 16 Apr 2004, christie wrote:
> 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
 
Reply With Quote
 
christie
Guest
Posts: n/a
 
      04-19-2004
Brad Baxter <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) s.uga.edu>...
> On Fri, 16 Apr 2004, christie wrote:
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Problem with foreach loop =?Utf-8?B?SnVzdGlu?= ASP .Net 2 10-22-2004 08:35 AM
Re: foreach loop error Luc Kumps ASP .Net 0 08-22-2003 10:16 PM
Re: foreach loop error Kevin Spencer ASP .Net 0 08-22-2003 09:11 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments