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

Re: [MacPerl] Unexpected Splice behavior?



xma@mill.net (Xiao-Jun Ma) writes:
}The following code produces the unexpected results. When it is run as
}written, it prints 2 3 4 as opposed to the expected 3 4. When the < sign in

I'm certainly glad that MacPerl correctly prints 2 3 4, because that's
exactly what Unix Perl 5.003 does under both SunOS 4.1.4 and HP-UX 9.05.
I'd suggest that maybe your expectations are wrong, and a second look at
your script is in order.  In particular, think about the value of $ary[1]
after one time through your loop, remembering that splice changes the @ary.

A minute in the debugger showed me the problem with your script.  It's a
useful tool for tracking down this kind of problem.

}the if line is changed to >, it prints 1 2 3 as expected. How come? A
}possible bug in Perl or MacPerl?
}
}I'm using the most recent version of MacPerl on LC III.
}
}Xiao-Jun Ma
}
}
}@a = (1,2,3);
}append(*a);
}
}sub append {
}   local(*ary) = @_;
}   push (@ary, 4);
}   for ($i = 0; $i < @ary; $i++) {
}      if ($ary[$i] < 3) {
}         splice(@ary, $i, 1);
}       }
}   }
}}
}
}print "@a\n";


---
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693, Greenbelt, MD 20771
schinder@pjstoaster.pg.md.us