Re: [Boston.pm] Loop index in foreach?

2006-09-29 Thread John Macdonald
On Tue, Sep 26, 2006 at 12:27:21PM +0100, David Cantrell wrote:
> On Mon, Sep 25, 2006 at 11:22:46PM -0400, John Macdonald wrote:
> > On Mon, Sep 25, 2006 at 12:35:49PM +0100, David Cantrell wrote:
> > > On Thu, Sep 21, 2006 at 07:02:56PM -0400, Duane Bronson top-posted:
> > > > Ronald J Kimball wrote:
> > > > > What would you have Perl do in the case of nested loops?
> > > > $.. should be the iterator count in the parent loop, $... should be the 
> > > > iterator count in the grandparent loop, ...
> > > Ick!  Better to use a stack, methinks, like I do for getting at outer
> > > map{}s' versions of $_ in NestedMap.
> > And even using a stack suffers from the flaw that any sort
> > of refactoring that puts a loop around a portion of the code
> > means search through the entire section and increasing the
> > index of any reference to a loop outside the new one ...
> 
> Any refactoring should be done carefully and with understanding of what
> the code you're refactoring is doing.  I'm not sure what your point is.

Wrapping a loop around a large chunk of code would change
the meaning of some of the variables within the chunk.
(The most likely candidate would be turning a single loop
into a nested pair of loops.  Instead of the code inside
the loop just working and only needing an extra indent, that
code would have to be scanned for all loop index references
and each one counted to see whether it is inside or outside
the change and adjusted accordingly.  If, instead of relative
names, the index for loops that needed index counters had been
given explicit names, the names would still refer to the same
loop index after the change and just continue to work correctly
without any examination and fixing required.  There's also the
potential for confusion about whether map, grep, and postfix
for get index counters (and what if the interpreter wanted to
just code one of them as an internal re-write that *did* use a
loop; and then the implementation changed).
)

-- 
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-27 Thread Ronald J Kimball
On Tue, Sep 26, 2006 at 12:27:21PM +0100, David Cantrell wrote:
> On Mon, Sep 25, 2006 at 11:22:46PM -0400, John Macdonald wrote:
> > On Mon, Sep 25, 2006 at 12:35:49PM +0100, David Cantrell wrote:
> > > On Thu, Sep 21, 2006 at 07:02:56PM -0400, Duane Bronson top-posted:
> > > > Ronald J Kimball wrote:
> > > > > What would you have Perl do in the case of nested loops?
> > > > $.. should be the iterator count in the parent loop, $... should be the 
> > > > iterator count in the grandparent loop, ...
> > > Ick!  Better to use a stack, methinks, like I do for getting at outer
> > > map{}s' versions of $_ in NestedMap.
> > And even using a stack suffers from the flaw that any sort
> > of refactoring that puts a loop around a portion of the code
> > means search through the entire section and increasing the
> > index of any reference to a loop outside the new one ...
> 
> Any refactoring should be done carefully and with understanding of what
> the code you're refactoring is doing.  I'm not sure what your point is.

Yes, it should.  That doesn't mean it will be.

More importantly, by using explicit loop index variables, you don't have to
do any refactoring at all.  That's why (in my opinion) having magic loop
index variables is a bad idea.


> And the bugs you introduce while doing it will, of course, be caught by
> your regression tests.

You're a glass-is-completely-full kind of guy, huh?


Ronald
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-26 Thread David Cantrell
On Mon, Sep 25, 2006 at 11:22:46PM -0400, John Macdonald wrote:
> On Mon, Sep 25, 2006 at 12:35:49PM +0100, David Cantrell wrote:
> > On Thu, Sep 21, 2006 at 07:02:56PM -0400, Duane Bronson top-posted:
> > > Ronald J Kimball wrote:
> > > > What would you have Perl do in the case of nested loops?
> > > $.. should be the iterator count in the parent loop, $... should be the 
> > > iterator count in the grandparent loop, ...
> > Ick!  Better to use a stack, methinks, like I do for getting at outer
> > map{}s' versions of $_ in NestedMap.
> And even using a stack suffers from the flaw that any sort
> of refactoring that puts a loop around a portion of the code
> means search through the entire section and increasing the
> index of any reference to a loop outside the new one ...

Any refactoring should be done carefully and with understanding of what
the code you're refactoring is doing.  I'm not sure what your point is.

And the bugs you introduce while doing it will, of course, be caught by
your regression tests.

-- 
David Cantrell | A machine for turning tea into grumpiness

  Irregular English:
you have anecdotes; they have data; I have proof
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-25 Thread John Macdonald
On Mon, Sep 25, 2006 at 12:35:49PM +0100, David Cantrell wrote:
> On Thu, Sep 21, 2006 at 07:02:56PM -0400, Duane Bronson top-posted:
> > Ronald J Kimball wrote:
> > > What would you have Perl do in the case of nested loops?
> > $.. should be the iterator count in the parent loop, $... should be the 
> > iterator count in the grandparent loop, ...
> 
> Ick!  Better to use a stack, methinks, like I do for getting at outer
> map{}s' versions of $_ in NestedMap.

And even using a stack suffers from the flaw that any sort
of refactoring that puts a loop around a portion of the code
means search through the entire section and increasing the
index of any reference to a loop outside the new one, while
not increasing the index of any reference to a loop within
the new one.  That's a good source of subtle errors there.

-- 
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-25 Thread Bill Ricker
> Ick!  Better to use a stack, methinks,

If one has to, yes... but good/better/best, Best is to avoid doing
index arithmetic if it isn't required -- use map()/grep() and other
list operations to process lists of lists of lists as lists will solve
many problems.

-- 
Bill
[EMAIL PROTECTED] [EMAIL PROTECTED]
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-25 Thread David Cantrell
On Thu, Sep 21, 2006 at 07:02:56PM -0400, Duane Bronson top-posted:
> Ronald J Kimball wrote:
> > What would you have Perl do in the case of nested loops?
> $.. should be the iterator count in the parent loop, $... should be the 
> iterator count in the grandparent loop, ...

Ick!  Better to use a stack, methinks, like I do for getting at outer
map{}s' versions of $_ in NestedMap.

-- 
David Cantrell | Reality Engineer, Ministry of Information

Planckton: n, the smallest possible living thing
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-22 Thread Palit, Nilanjan
For nested loops, the index reported would be the innermost loop for the
context:

Let's say the special auto loop index variable is: $^Z

foreach (@array1)
{
print "Loop1: index=$^Z\n"; #Should report index for loop1
foreach (@array2)
{
print "Loop2: index=$^Z\n"; #Should report index for
loop2
foreach (@array3)
{
print "Loop3: index=$^Z\n"; #Should report index for
loop3
...
}
}
}

If an user wants to use an outer loop's index inside an inner loop, they
can store it in a variable when in that loop's context.

-Nilanjan


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Ronald J Kimball
Sent: Thursday, September 21, 2006 6:47 PM
To: Palit, Nilanjan
Cc: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

On Thu, Sep 21, 2006 at 09:34:43AM -0700, Palit, Nilanjan wrote:

> I think it'd be fairly easy for Perl to auto initialize & increment a
> loop index in all loops & provide that to the user in a special
> variable. "$." is an excellent example. I think it'd be a great
addition
> to Perl's excellent (& long) list of special vars, making for yet more
> elegant & concise code.

What would you have Perl do in the case of nested loops?

Ronald
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-22 Thread Tolkin, Steve
Are you serious?  $.., $..., $ etc?!  Aii!!! he screams and runs away.  
Please stop this thread.

Hopefully helpfully yours, 
Steve 
-- 
Steve Tolkin    Steve . Tolkin at FMR dot COM   508-787-9006
Fidelity Investments   82 Devonshire St. M3L Boston MA 02109 
There is nothing so practical as a good theory.  Comments are by me, 
not Fidelity Investments, its subsidiaries or affiliates. 



 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Duane Bronson
Sent: Thursday, September 21, 2006 7:03 PM
To: Ronald J Kimball
Cc: boston-pm@mail.pm.org; Palit, Nilanjan
Subject: Re: [Boston.pm] Loop index in foreach?

$.. should be the iterator count in the parent loop, $... should be the 
iterator count in the grandparent loop, ...

my @fruits = ('apple','banana','cantaloupe');
foreach my $fruit  (@fruits) {
  foreach my $minusone (0..1000) {
foreach my $plusone (2..1000) {
  die "inner loop count wrong" unless $plusone == $.+1;
  die "outer loop count wrong" unless $minusone == $..-1;
  die "way outer loop index wrong" unless $fruit eq $fruits[$...];
}
  }

Ronald J Kimball wrote:
> On Thu, Sep 21, 2006 at 09:34:43AM -0700, Palit, Nilanjan wrote:
>
>   
>> I think it'd be fairly easy for Perl to auto initialize & increment a
>> loop index in all loops & provide that to the user in a special
>> variable. "$." is an excellent example. I think it'd be a great addition
>> to Perl's excellent (& long) list of special vars, making for yet more
>> elegant & concise code.
>> 
>
> What would you have Perl do in the case of nested loops?
>
> Ronald
>  
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm
>
>   

-- 
Sincerely   *Duane Bronson*
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
http://www.nerdlogic.com/
453 Washington St. #4A, Boston, MA 02111
617.515.2909

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Bob Rogers
   From: "Palit, Nilanjan" <[EMAIL PROTECTED]>
   Date: Thu, 21 Sep 2006 12:14:54 -0700

   How 'bout this:

   my $i= -1;
   map { print "at index '$_->[0]', value='$_->[1]'\n"; } map { $i++; [$i,
   $_] } @array;

   But as before, none of these workarounds are quite as elegant or
   efficient as the implicit "$." semantic ...

   -Nilanjan

I don't follow.  Given that you've already defined "elegant" and
"efficient" as "less code," here's a version of your Verilog parsing
example:

my $i = 0;
foreach (@sigs) {
$mybus{"bus1[$i]"} = $_;
$i++;
}

A hypothetical $. would indeed make it more compact:

foreach (@sigs) {
$mybus{"bus1[$.]"} = $_;
}

But using an explicit index is just as compact, though it does require
more tokens:

for my $i (0..$#sigs) {
$mybus{"bus1[$i]"} = $sigs[$i];
}

And Uri's slice assignment is in fact shorter:

@mybus{ map "bus1[$_]", 0..$#sigs } = @sigs;

Using functional assignment is almost as concise (though it's not
equivalent if %mybus is already populated):

%mybus = map { "bus1[$_]" => $sigs[$_]; } 0..$#sigs;

Of course, some of this depends on my coding style -- I rewrote all of
these examples the way I normally code so that they could be compared
directly; YMMV, naturally.  But I just don't see the need for another
line-noise variable in Perl.

-- Bob Rogers
   http://rgrjr.dyndns.org/
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Duane Bronson
$.. should be the iterator count in the parent loop, $... should be the 
iterator count in the grandparent loop, ...

my @fruits = ('apple','banana','cantaloupe');
foreach my $fruit  (@fruits) {
  foreach my $minusone (0..1000) {
foreach my $plusone (2..1000) {
  die "inner loop count wrong" unless $plusone == $.+1;
  die "outer loop count wrong" unless $minusone == $..-1;
  die "way outer loop index wrong" unless $fruit eq $fruits[$...];
}
  }

Ronald J Kimball wrote:
> On Thu, Sep 21, 2006 at 09:34:43AM -0700, Palit, Nilanjan wrote:
>
>   
>> I think it'd be fairly easy for Perl to auto initialize & increment a
>> loop index in all loops & provide that to the user in a special
>> variable. "$." is an excellent example. I think it'd be a great addition
>> to Perl's excellent (& long) list of special vars, making for yet more
>> elegant & concise code.
>> 
>
> What would you have Perl do in the case of nested loops?
>
> Ronald
>  
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm
>
>   

-- 
Sincerely   *Duane Bronson*
[EMAIL PROTECTED] 
http://www.nerdlogic.com/
453 Washington St. #4A, Boston, MA 02111
617.515.2909

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Ronald J Kimball
On Thu, Sep 21, 2006 at 09:34:43AM -0700, Palit, Nilanjan wrote:

> I think it'd be fairly easy for Perl to auto initialize & increment a
> loop index in all loops & provide that to the user in a special
> variable. "$." is an excellent example. I think it'd be a great addition
> to Perl's excellent (& long) list of special vars, making for yet more
> elegant & concise code.

What would you have Perl do in the case of nested loops?

Ronald
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Palit, Nilanjan
How 'bout this:

my $i= -1;
map { print "at index '$_->[0]', value='$_->[1]'\n"; } map { $i++; [$i,
$_] } @array;

But as before, none of these workarounds are quite as elegant or
efficient as the implicit "$." semantic ...

-Nilanjan


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Greg London
Sent: Thursday, September 21, 2006 12:53 PM
To: Palit, Nilanjan; boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

A few years ago, I wrote a module called Loop.pm
which I believe does what you want.
 
http://cpan.uwinnipeg.ca/~gslondon/Loop
 
Here's how you Loop through an array:

use Loop; 

my @array = qw (alpha bravo charlie); 

Loop::Array @array, sub 
{
my ($index,$value)[EMAIL PROTECTED];
print "at index '$index', value='$value'\n";
}


This example will return a list of keys that are contained in their
corresponding value. 

my %hash = qw 
(
aaa bebaaa
abc xyz
roottaproot
oingo   boingo
billy   bob
);


@key_is_in_value = Loop::Hash %hash, sub 
{
my($key,$val)[EMAIL PROTECTED];
if($val=~/$key/)
{return $key;}
else
{return;}
};



Hashes can have nested loops, which avoids the perl builtin "each"
problem of having a place holder for only one iterator. 

Files can be looped through providing a quick and easy way to open,
read, and close a file, including error handling on failures to open,
and providing the current line number for your code. 

It does all the work for you in one call. 

# print first 10 lines of file, along with each line's line number. 

Loop::File "tfile.pl", sub 
{
my($linenum,$linestr)[EMAIL PROTECTED];
print "$linenum \t: $linestr";
$_[-1]='last' if ($linenum == 10);
};
All is not perfect in Loop.pm, though.
Constructs such as "next" and "last"
are not available, since it isn't really
a lexical loop that the parser can find.
Instead, what you do is assign a string
to $_[-1], and the string will be 'next'
or 'last'. The looping wrapper will then
do the right thing.
 
Greg
 
 
________

From: [EMAIL PROTECTED] on behalf of
Palit, Nilanjan
Sent: Thu 9/21/2006 12:34 PM
To: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?



Uri,

Using both the array element value & index in a loop is a very
fundamental coding usage (just like using the hash key & value), which I
didn't think needed further elaboration (it's not exactly an esoteric
usage model!). Nevertheless, here is an  example I'm currently working
on (which prompted the question):

I'm parsing a Verilog assign statement & the RHS is a multi-bit
concatenated bus. The key on the LHS includes the "[$i]", so I can use
that to correlate the same signal to another place in the code:

   my $i= 0;
   foreach (@sigs)
   { $mybus{"bus1[$i]"}= $_; $i++; }


I think it'd be fairly easy for Perl to auto initialize & increment a
loop index in all loops & provide that to the user in a special
variable. "$." is an excellent example. I think it'd be a great addition
to Perl's excellent (& long) list of special vars, making for yet more
elegant & concise code.


The "push(@newarray[$_], ..." in my original example was a bad typo -- I
did not intend the '[$_]' ... Thanks for flagging that.

Thanks,

-Nilanjan


-Original Message-
From: Uri Guttman [mailto:[EMAIL PROTECTED]
Sent: Monday, September 18, 2006 12:09 PM
To: Palit, Nilanjan
Cc: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

>>>>> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:

  PN> That works too (& I have used it as well), but I guess what I'm
  PN> really asking (hoping?) for is a Perl special variable, kinda like
  PN> "$.", which Perl auto-magically initializes & increments ...

in my experience classic loop indexes are rarely needed in perl. my take
is that coders who want an index value haven't done a proper design. if
you explain the larger problem i bet we could show you ways to bypass
your perceived need for a loop index.

also in your example you are pushing to @newarray[$i] which is not good
perl as it is an array slice with a single index. this will trigger a
warning if you enable warnings. if all you wanted to do was push some
values onto an new array, then map is your answer as someone else
showed. but anyhow we would like to see more info about the problem so
we can help solve it at the proper level.

uri

--
Uri Guttman  

Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Uri Guttman
> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:

  PN> Uri,
  PN> Using both the array element value & index in a loop is a very
  PN> fundamental coding usage (just like using the hash key & value), which I
  PN> didn't think needed further elaboration (it's not exactly an esoteric
  PN> usage model!). Nevertheless, here is an  example I'm currently working
  PN> on (which prompted the question):

please, i do know a little about looping as i am very loopy most of the
time! :)

my point is that in perl (but not in almost any other lang), loop
indexing is rarely needed. i have written thousands of lines of
production perl and i can barely count the times i use loop indexes. i
always design my data structures to not need them as it is cleaner and
faster to not use them. in most other langs loop indexes are practically
required as you don't have a proper loop over a list construct like
foreach. 

  PN> I'm parsing a Verilog assign statement & the RHS is a multi-bit
  PN> concatenated bus. The key on the LHS includes the "[$i]", so I can use
  PN> that to correlate the same signal to another place in the code:

  PN>my $i= 0;
  PN>foreach (@sigs)
  PN>{ $mybus{"bus1[$i]"}= $_; $i++; }

that can be done with for modifier and a range assuming @sigs can be
destroyed with shifts:

$mybus{ "bus1[$_]" } = shift @sigs for 0 .. $#sigs ;

here is another way to do it:

@mybus{ map "bus1[$_]", 0 .. $#sigs } = @sigs ;

shorter and non-destructive on @sigs. i like slices as another way to
avoid loop indexing.

seeing ++ on a loop index var is also a red flag that tells me to use a
range operator if possible. then you don't need to worry about ++ being
in the right place and you can put the loop control in a better place.

  PN> I think it'd be fairly easy for Perl to auto initialize & increment a
  PN> loop index in all loops & provide that to the user in a special
  PN> variable. "$." is an excellent example. I think it'd be a great addition
  PN> to Perl's excellent (& long) list of special vars, making for yet more
  PN> elegant & concise code.

ain't gonna happen in perl5. perl6 has such a beast.

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs    http://jobs.perl.org
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Greg London
A few years ago, I wrote a module called Loop.pm
which I believe does what you want.
 
http://cpan.uwinnipeg.ca/~gslondon/Loop
 
Here's how you Loop through an array:

use Loop; 

my @array = qw (alpha bravo charlie); 

Loop::Array @array, sub 
{
my ($index,$value)[EMAIL PROTECTED];
print "at index '$index', value='$value'\n";
}


This example will return a list of keys that are contained in their 
corresponding value. 

my %hash = qw 
(
aaa bebaaa
abc xyz
roottaproot
oingo   boingo
billy   bob
);


@key_is_in_value = Loop::Hash %hash, sub 
{
my($key,$val)[EMAIL PROTECTED];
if($val=~/$key/)
{return $key;}
else
{return;}
};



Hashes can have nested loops, which avoids the perl builtin "each" problem of 
having a place holder for only one iterator. 

Files can be looped through providing a quick and easy way to open, read, and 
close a file, including error handling on failures to open, and providing the 
current line number for your code. 

It does all the work for you in one call. 

# print first 10 lines of file, along with each line's line number. 

Loop::File "tfile.pl", sub 
{
my($linenum,$linestr)[EMAIL PROTECTED];
print "$linenum \t: $linestr";
$_[-1]='last' if ($linenum == 10);
};
All is not perfect in Loop.pm, though.
Constructs such as "next" and "last"
are not available, since it isn't really
a lexical loop that the parser can find.
Instead, what you do is assign a string
to $_[-1], and the string will be 'next'
or 'last'. The looping wrapper will then
do the right thing.
 
Greg
 
 
________

From: [EMAIL PROTECTED] on behalf of Palit, Nilanjan
Sent: Thu 9/21/2006 12:34 PM
To: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?



Uri,

Using both the array element value & index in a loop is a very
fundamental coding usage (just like using the hash key & value), which I
didn't think needed further elaboration (it's not exactly an esoteric
usage model!). Nevertheless, here is an  example I'm currently working
on (which prompted the question):

I'm parsing a Verilog assign statement & the RHS is a multi-bit
concatenated bus. The key on the LHS includes the "[$i]", so I can use
that to correlate the same signal to another place in the code:

   my $i= 0;
   foreach (@sigs)
   { $mybus{"bus1[$i]"}= $_; $i++; }


I think it'd be fairly easy for Perl to auto initialize & increment a
loop index in all loops & provide that to the user in a special
variable. "$." is an excellent example. I think it'd be a great addition
to Perl's excellent (& long) list of special vars, making for yet more
elegant & concise code.


The "push(@newarray[$_], ..." in my original example was a bad typo -- I
did not intend the '[$_]' ... Thanks for flagging that.

Thanks,

-Nilanjan


-Original Message-
From: Uri Guttman [mailto:[EMAIL PROTECTED]
Sent: Monday, September 18, 2006 12:09 PM
To: Palit, Nilanjan
Cc: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

>>>>> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:

  PN> That works too (& I have used it as well), but I guess what I'm
  PN> really asking (hoping?) for is a Perl special variable, kinda like
  PN> "$.", which Perl auto-magically initializes & increments ...

in my experience classic loop indexes are rarely needed in perl. my take
is that coders who want an index value haven't done a proper design. if
you explain the larger problem i bet we could show you ways to bypass
your perceived need for a loop index.

also in your example you are pushing to @newarray[$i] which is not good
perl as it is an array slice with a single index. this will trigger a
warning if you enable warnings. if all you wanted to do was push some
values onto an new array, then map is your answer as someone else
showed. but anyhow we would like to see more info about the problem so
we can help solve it at the proper level.

uri

--
Uri Guttman  --  [EMAIL PROTECTED]  
http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and
Coding-
Search or Offer Perl Jobs  
http://jobs.perl.org

___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-21 Thread Palit, Nilanjan
Uri,

Using both the array element value & index in a loop is a very
fundamental coding usage (just like using the hash key & value), which I
didn't think needed further elaboration (it's not exactly an esoteric
usage model!). Nevertheless, here is an  example I'm currently working
on (which prompted the question):

I'm parsing a Verilog assign statement & the RHS is a multi-bit
concatenated bus. The key on the LHS includes the "[$i]", so I can use
that to correlate the same signal to another place in the code:

   my $i= 0;
   foreach (@sigs)
   { $mybus{"bus1[$i]"}= $_; $i++; }


I think it'd be fairly easy for Perl to auto initialize & increment a
loop index in all loops & provide that to the user in a special
variable. "$." is an excellent example. I think it'd be a great addition
to Perl's excellent (& long) list of special vars, making for yet more
elegant & concise code.


The "push(@newarray[$_], ..." in my original example was a bad typo -- I
did not intend the '[$_]' ... Thanks for flagging that.

Thanks,

-Nilanjan


-Original Message-
From: Uri Guttman [mailto:[EMAIL PROTECTED] 
Sent: Monday, September 18, 2006 12:09 PM
To: Palit, Nilanjan
Cc: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

>>>>> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:

  PN> That works too (& I have used it as well), but I guess what I'm
  PN> really asking (hoping?) for is a Perl special variable, kinda like
  PN> "$.", which Perl auto-magically initializes & increments ...

in my experience classic loop indexes are rarely needed in perl. my take
is that coders who want an index value haven't done a proper design. if
you explain the larger problem i bet we could show you ways to bypass
your perceived need for a loop index.

also in your example you are pushing to @newarray[$i] which is not good
perl as it is an array slice with a single index. this will trigger a
warning if you enable warnings. if all you wanted to do was push some
values onto an new array, then map is your answer as someone else
showed. but anyhow we would like to see more info about the problem so
we can help solve it at the proper level.

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]  
http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and
Coding-
Search or Offer Perl Jobs  
http://jobs.perl.org
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Andy Lester

On Sep 18, 2006, at 3:53 PM, Shlomi Fish wrote:

> Well, here is my attempt at writing such an iterator. Using it one  
> gets the
> following program:

 which only demonstrates so clearly that it's far far better to  
simply

my $n = 0;
for ( @array ) {
++$n;

}

Flyswatter vs. shotgun

--
Andy Lester => [EMAIL PROTECTED] => www.petdance.com => AIM:petdance




 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Shlomi Fish
On Monday 18 September 2006 18:43, Kenneth A Graves wrote:
> On Mon, 2006-09-18 at 08:28 -0700, Palit, Nilanjan wrote:
> > That works too (& I have used it as well), but I guess what I'm really
> > asking (hoping?) for is a Perl special variable, kinda like "$.", which
> > Perl auto-magically initializes & increments ...
>
> Perl6 will have a way of iterating over indices and values at the same
> time.  I don't think anyone has back-ported it though.  

Hi all.

Well, here is my attempt at writing such an iterator. Using it one gets the 
following program:

<<<<<<<<<<<<<<
#!/usr/bin/perl

use strict;
use warnings;

use ArrayIter;

my @array = ("One Fish", "Two Fish", "Red Fish", "Blue Fish");

my $it = ArrayIter->new([EMAIL PROTECTED]);
while (my ($idx, $val) = $it->next())
{
print "array[$idx] = $val\n";
}
>>>>>>>>>>>>>>

To output:

<<<<<<<<<<<
array[0] = One Fish
array[1] = Two Fish
array[2] = Red Fish
array[3] = Blue Fish
>>>>>>>>>>>

And here's the module code:

<<<<<<<<<<<<<
package ArrayIter;

use strict;
use warnings;

use base 'Class::Accessor';

__PACKAGE__->mk_accessors(qw(
array_ref
idx
));

sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_init(@_);
return $self;
}

sub _init
{
my $self = shift;
my $array_ref = shift;

$self->array_ref($array_ref);
$self->idx(0);

return 0;
}

sub next
{
my $self = shift;

if ($self->idx() == @{$self->array_ref()})
{
return;
}
else
{
my @ret = ($self->idx(), $self->array_ref->[$self->idx()]);
$self->idx($self->idx()+1);
# TODO : Do something meaningful for scalar context.
return @ret;
}
}
1;
>>>>>>>>>>>>>

Everything here is free for any use under the MIT X11 licence. (a BSD-style 
Public Domain-like licence).

Of course I'm not sure it's actually any improvement, but who is John Galt?

Regards,

Shlomi Fish

BTW: the module and script worked perfectly after the first run, without a 
single error. But there may still be some bugs.

> The various 
> somewhat cumbersome ways to code it are your best approaches for now, if
> you really need the index.
>
> Note that you might be able to get away without the index, with a
> suitable rearrangement of your code:
>
> @newarray = map {complicated_function($_)} @array;
>
> --kag
>
> > -Original Message-
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On
> > Behalf Of Dan Boger
> > Sent: Monday, September 18, 2006 10:39 AM
> > To: boston-pm@mail.pm.org
> > Subject: Re: [Boston.pm] Loop index in foreach?
> >
> > On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> > > In a foreach loop, is there a way to find out the loop index number?
> > > E.g.:
> > >
> > > foreach (@myarray)
> > > {
> > > ...
> > > push(@newarray[], );
> > > ...
> > > }
> > >
> > > Currently, I have to resort to the following:
> > > for (my $i= 0; $i <= $#myarray, $i++)
> > > { ... push(@newarray[$i], ); ...}
> > >
> > >  ... which is more wordy -- I was wondering if there's a more
> > > elegant/efficient (i.e. less code) to do this?
> >
> > Not sure if I'd call this more elegant, but perhaps
> >
> > my $index = 0;
> > foreach (@myarray) {
> >   ...
> >   push(@newarray[$index], );
> >   ...
> >
> >   $index++;
> > }
>
> ___
> Boston-pm mailing list
> Boston-pm@mail.pm.org
> http://mail.pm.org/mailman/listinfo/boston-pm

-- 

-
Shlomi Fish  [EMAIL PROTECTED]
Homepage:http://www.shlomifish.org/

Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Dan Boger
On Mon, Sep 18, 2006 at 08:28:33AM -0700, Palit, Nilanjan wrote:
> That works too (& I have used it as well), but I guess what I'm really
> asking (hoping?) for is a Perl special variable, kinda like "$.",
> which Perl auto-magically initializes & increments ...

I doubt it does - considering that foreach doesn't work on arrays, but
on lists.  So all such a magic variable can tell you is how many
iterations the loop has ran so far, which is the same as the two lines
of code in the earlier post.

Dan

-- 
Dan Boger
[EMAIL PROTECTED]
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Uri Guttman
> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:

  PN> That works too (& I have used it as well), but I guess what I'm
  PN> really asking (hoping?) for is a Perl special variable, kinda like
  PN> "$.", which Perl auto-magically initializes & increments ...

in my experience classic loop indexes are rarely needed in perl. my take
is that coders who want an index value haven't done a proper design. if
you explain the larger problem i bet we could show you ways to bypass
your perceived need for a loop index.

also in your example you are pushing to @newarray[$i] which is not good
perl as it is an array slice with a single index. this will trigger a
warning if you enable warnings. if all you wanted to do was push some
values onto an new array, then map is your answer as someone else
showed. but anyhow we would like to see more info about the problem so
we can help solve it at the proper level.

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs    http://jobs.perl.org
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Kenneth A Graves
On Mon, 2006-09-18 at 08:28 -0700, Palit, Nilanjan wrote:
> That works too (& I have used it as well), but I guess what I'm really
> asking (hoping?) for is a Perl special variable, kinda like "$.", which
> Perl auto-magically initializes & increments ...

Perl6 will have a way of iterating over indices and values at the same
time.  I don't think anyone has back-ported it though.  The various
somewhat cumbersome ways to code it are your best approaches for now, if
you really need the index.

Note that you might be able to get away without the index, with a
suitable rearrangement of your code:

@newarray = map {complicated_function($_)} @array;

--kag

> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of Dan Boger
> Sent: Monday, September 18, 2006 10:39 AM
> To: boston-pm@mail.pm.org
> Subject: Re: [Boston.pm] Loop index in foreach?
> 
> On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> > In a foreach loop, is there a way to find out the loop index number?
> > E.g.:
> > 
> > foreach (@myarray)
> > {
> > ...
> > push(@newarray[], );
> > ...
> > }
> > 
> > Currently, I have to resort to the following:
> > for (my $i= 0; $i <= $#myarray, $i++)
> > { ... push(@newarray[$i], ); ...}
> > 
> >  ... which is more wordy -- I was wondering if there's a more
> > elegant/efficient (i.e. less code) to do this?
> 
> Not sure if I'd call this more elegant, but perhaps
> 
> my $index = 0;
> foreach (@myarray) {
>   ...
>   push(@newarray[$index], );
>   ...
> 
>   $index++;
> }
>   
> 

 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Palit, Nilanjan
That works too (& I have used it as well), but I guess what I'm really
asking (hoping?) for is a Perl special variable, kinda like "$.", which
Perl auto-magically initializes & increments ...

-Nilanjan


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Dan Boger
Sent: Monday, September 18, 2006 10:39 AM
To: boston-pm@mail.pm.org
Subject: Re: [Boston.pm] Loop index in foreach?

On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> In a foreach loop, is there a way to find out the loop index number?
> E.g.:
> 
> foreach (@myarray)
> {
> ...
> push(@newarray[], );
> ...
> }
> 
> Currently, I have to resort to the following:
> for (my $i= 0; $i <= $#myarray, $i++)
> { ... push(@newarray[$i], ); ...}
> 
>  ... which is more wordy -- I was wondering if there's a more
> elegant/efficient (i.e. less code) to do this?

Not sure if I'd call this more elegant, but perhaps

my $index = 0;
foreach (@myarray) {
  ...
  push(@newarray[$index], );
  ...

  $index++;
}
  

-- 
Dan Boger
[EMAIL PROTECTED]
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Dan Boger
On Mon, Sep 18, 2006 at 07:30:40AM -0700, Palit, Nilanjan wrote:
> In a foreach loop, is there a way to find out the loop index number?
> E.g.:
> 
> foreach (@myarray)
> {
> ...
> push(@newarray[], );
> ...
> }
> 
> Currently, I have to resort to the following:
> for (my $i= 0; $i <= $#myarray, $i++)
> { ... push(@newarray[$i], ); ...}
> 
>  ... which is more wordy -- I was wondering if there's a more
> elegant/efficient (i.e. less code) to do this?

Not sure if I'd call this more elegant, but perhaps

my $index = 0;
foreach (@myarray) {
  ...
  push(@newarray[$index], );
  ...

  $index++;
}
  

-- 
Dan Boger
[EMAIL PROTECTED]
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm


Re: [Boston.pm] Loop index in foreach?

2006-09-18 Thread Palit, Nilanjan
Forgot to add: The second alternative is also wordy:

foreach (0 .. $#myarray)
{
...
push(@newarray[$_], );
...
}

And just like the for-loop, I longer have the array element in $_, which
makes all implicit $_ operations (regex's, chomp's, ...) wordier ...

-Nilanjan


> _ 
> From: Palit, Nilanjan  
> Sent: Monday, September 18, 2006 10:31 AM
> To:   L-boston-pm
> Subject:  Loop index in foreach?
> 
> In a foreach loop, is there a way to find out the loop index number?
> E.g.:
> 
> foreach (@myarray)
> {
> ...
> push(@newarray[], );
> ...
> }
> 
> Currently, I have to resort to the following:
> for (my $i= 0; $i <= $#myarray, $i++)
> { ... push(@newarray[$i], ); ...}
> 
>  ... which is more wordy -- I was wondering if there's a more
> elegant/efficient (i.e. less code) to do this?
> Thanks,
> 
> -Nilanjan
> 
> 
> Nilanjan Palit
> __
> Infrastructure Processor Division, CIG/DEG
> Intel Massachusetts Development Center
> Email: [EMAIL PROTECTED]
> 
 
___
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm