Position in an array

Discussion in 'Perl Misc' started by bernd, Jan 16, 2007.

  1. bernd

    bernd Guest

    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
     
    bernd, Jan 16, 2007
    #1
    1. Advertising

  2. bernd wrote:

    > $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;
    }

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' ', Web => ' http://474.at ' }
     
    Robert 'phaylon' Sedlacek, Jan 16, 2007
    #2
    1. Advertising

  3. bernd

    Mirco Wahab Guest

    Robert 'phaylon' Sedlacek wrote:
    > bernd wrote:
    >> $count = 0 ;
    >> foreach ( @testarr ) {
    >> if ( $count == 0 ) { print $_ }

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



    foreach ( @testarr ) {
    print if $# == 0;
    }

    perl6?


    Regards

    Mirco
     
    Mirco Wahab, Jan 16, 2007
    #3
  4. Robert 'phaylon' Sedlacek wrote:

    > 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;
    }

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' ', Web => ' http://474.at ' }
     
    Robert 'phaylon' Sedlacek, Jan 16, 2007
    #4
  5. bernd

    -berlin.de Guest

    bernd <> wrote in comp.lang.perl.misc:
    > 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
     
    -berlin.de, Jan 16, 2007
    #5
  6. bernd

    tfe Guest

    Robert 'phaylon' Sedlacek a écrit :
    > bernd wrote:
    >
    > > $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;
    > }
    >


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

    --
    tfe
     
    tfe, Jan 16, 2007
    #6
  7. bernd

    -berlin.de Guest

    tfe <> wrote in comp.lang.perl.misc:
    >
    > Robert 'phaylon' Sedlacek a écrit :
    > > bernd wrote:
    > >
    > > > $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;
    > > }
    > >

    >
    > 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
     
    -berlin.de, Jan 16, 2007
    #7
  8. bernd

    bernd Guest

    @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
     
    bernd, Jan 16, 2007
    #8
  9. bernd wrote:
    > $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) {
    # ...
    }
     
    RedGrittyBrick, Jan 16, 2007
    #9
  10. bernd

    bernd Guest

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

    On Jan 16, 9:50 pm, RedGrittyBrick <>
    wrote:
    > bernd wrote:
    > > $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 -
     
    bernd, Jan 16, 2007
    #10
  11. bernd

    J. Gleixner Guest

    bernd wrote:

    Fixed top-post...
    >
    > On Jan 16, 9:50 pm, RedGrittyBrick <>
    > wrote:
    >> bernd wrote:
    >>> $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.
     
    J. Gleixner, Jan 16, 2007
    #11
  12. 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";
     
    RedGrittyBrick, Jan 16, 2007
    #12
  13. bernd

    Dr.Ruud Guest

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



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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 16, 2007
    #13
  14. bernd

    Mark Guest

    bernd wrote:
    > 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] ;
    }
     
    Mark, Jan 17, 2007
    #14
    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. Luqman
    Replies:
    1
    Views:
    676
    Luqman
    Feb 7, 2006
  2. James Wong
    Replies:
    4
    Views:
    533
    James Wong
    Jul 14, 2004
  3. Replies:
    3
    Views:
    195
  4. James Black
    Replies:
    0
    Views:
    409
    James Black
    May 28, 2006
  5. brendan
    Replies:
    0
    Views:
    213
    brendan
    Aug 29, 2006
Loading...

Share This Page