A question of Style, was: SPUG: Sort an array question

Michael R. Wolf MichaelRunningWolf at att.net
Fri Dec 27 16:17:59 CST 2002


Brian Hatch <spug at ifokr.org> writes:

> 	for my $i ( 0 .. $#array) {
> 	    for my $j ( $i+1 .. $#array) {
> 		$tmp = $array[$j] . $array[$i];	  # join compare elements> 
> Why?  $i and $j are scoped only inside this block.  

While I agree with the short variable names, beware the depricated
$#array syntax.  It's not too well known and it's going away.  That's
two strikes against it in my book.

Since I don't really like this off-by-one syntax,

	for my $i ( 0 .. @array-1) {
	    for my $j ( $i+1 .. @array-1) {

my personal preference would be to go with the 3-part for

	for (my $i = 0; $i < @array; $i++) {
	    for (my $j = $i+1; $j < @array; $j++) {

since it's such a well-known idiom.

AND -- I do like your exploration of the seemingly simpler syntax.
The following just *looks* like "for $ getting the legal indices of
@array" (except that the end-point is grotty).

	for my $i ( 0 .. @array-1) {

-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list