in reply to Auto-increment behaviour, is this normal and why?
For what you want, you probably mean:$i=0; if ($i++) { print 'first=' , $i, ', '; } if ($i++) { print 'middle=' , $i, ', '; } if ($i++) { print 'last=' , $i, ', '; } if ($i++) { print 'beyond=' , $i, ', '; }
which is admittedly a little awkward. More natural would be to post-increment after your print, except you clearly intend the incrementation to happen independently of whether the print happens. Perhaps$i=0; if ($i++) { print 'first=' , $i - 1, ', '; } if ($i++) { print 'middle=' , $i - 1, ', '; } if ($i++) { print 'last=' , $i - 1, ', '; } if ($i++) { print 'beyond=' , $i - 1, ', '; }
or$i=0; print 'first=' , $i, ', ' if $i; $i++; print 'middle=', $i, ', ' if $i; $i++; print 'last=' , $i, ', ' if $i; $i++; print 'beyond=', $i if $i; $i++;
my $i=0; outputter('first', $i++); outputter('middle', $i++); outputter('last', $i++); outputter('beyond', $i++); sub outputter { my ($txt, $i) = @_; print "$txt=$i, " if $i; }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Auto-increment behaviour, is this normal and why?
by Gilles Maisonneuve (Initiate) on Oct 19, 2013 at 03:33 UTC |