Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Position in an array

Reply
Thread Tools

Position in an array

 
 
bernd
Guest
Posts: n/a
 
      01-16-2007
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

 
Reply With Quote
 
 
 
 
Robert 'phaylon' Sedlacek
Guest
Posts: n/a
 
      01-16-2007
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 => ' http://www.velocityreviews.com/forums/(E-Mail Removed) ', Web => ' http://474.at ' }
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      01-16-2007
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
 
Reply With Quote
 
Robert 'phaylon' Sedlacek
Guest
Posts: n/a
 
      01-16-2007
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 => ' (E-Mail Removed) ', Web => ' http://474.at ' }
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      01-16-2007
bernd <(E-Mail Removed)> 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
 
Reply With Quote
 
tfe
Guest
Posts: n/a
 
      01-16-2007

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

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      01-16-2007
tfe <(E-Mail Removed)> 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
 
Reply With Quote
 
bernd
Guest
Posts: n/a
 
      01-16-2007
@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

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-16-2007
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) {
# ...
}
 
Reply With Quote
 
bernd
Guest
Posts: n/a
 
      01-16-2007

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

On Jan 16, 9:50 pm, RedGrittyBrick <(E-Mail Removed)>
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 -


 
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
Each Array Method Skipping first Array position Chris R. Ruby 3 01-28-2011 04:00 PM
detecting mouse click position in canvas tag has position off James Black Javascript 0 05-28-2006 03:27 AM
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Luqman ASP .Net 1 02-07-2006 10:27 AM
position image based on document position edouard.lauer@pt.lu Javascript 3 01-14-2006 06:04 PM
How to set position of a web control depending on other control's position at run-time? James Wong ASP .Net Web Controls 4 07-14-2004 10:24 AM



Advertisments