[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] $ids[$i++]



I've been doing this for years:

my $i = 0;
foreach my $single_reason (split /\|/, $reason) {
  my $id = $ids[$i];
  $i++;
  ...


But now my C++ mid-term I just took last night has me thinking like this:

my $i = 0;
foreach my $single_reason (split /\|/, $reason) {
  my $id = $ids[$i++];
  ...


Survey: Is the potential confusion between ++$i and $i++ worth saving 1 line of code?

So far I'm voting "no"...?

Ponder,

j



perldoc perlop

Auto-increment and Auto-decrement

"++" and "--" work as in C.  That is, if placed before a
variable, they increment or decrement the variable by one
before returning the value, and if placed after, increment
or decrement after returning the value.

 $i = 0;  $j = 0;
 print $i++;  # prints 0
 print ++$j;  # prints 1