foreach loop test

Discussion in 'Perl Misc' started by Robin, Apr 13, 2004.

  1. Robin

    Robin Guest

    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
    Robin, Apr 13, 2004
    #1
    1. Advertising

  2. Robin

    Matija Papec Guest

    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
    Matija Papec, Apr 13, 2004
    #2
    1. Advertising

  3. Robin

    Steven Kuo Guest

    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
    Steven Kuo, Apr 13, 2004
    #3
  4. 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(0x80d0cf8) 1
    last 1


    gtoomey
    Gregory Toomey, Apr 14, 2004
    #4
  5. Robin

    Tore Aursand Guest

    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 <>
    "I am become Death, shatterer of worlds." -- J. Robert Oppenheimer,
    upon witnessing the explosion of the first atomic bomb.
    Tore Aursand, Apr 14, 2004
    #5
  6. 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)
    Jeff 'japhy' Pinyan, Apr 14, 2004
    #6
  7. Robin

    Ted Zlatanov Guest

    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
    Ted Zlatanov, Apr 14, 2004
    #7
  8. Robin

    christie Guest

    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 <> wrote in message news:<>...
    > 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
    christie, Apr 16, 2004
    #8
  9. Robin

    Brad Baxter Guest

    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
    Brad Baxter, Apr 17, 2004
    #9
  10. Robin

    christie Guest

    Brad Baxter <> wrote in message news:<>...
    > 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
    christie, Apr 19, 2004
    #10
  11. Robin

    Brad Baxter Guest

    On Mon, 19 Apr 2004, christie wrote:

    > Brad Baxter <> wrote in message news:<>...
    > > 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.



    You're welcome. :)

    Brad
    Brad Baxter, Apr 20, 2004
    #11
  12. Robin

    Robin Guest

    "Robin" <robin @ infusedlight.net> wrote in message
    news:c5ho9l$cgt$...
    > 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
    >


    thanks for all the info, I'll use the for loop...
    -Robin
    Robin, Apr 27, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    319
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    739
    Scott Allen
    Feb 12, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page