Here's another "What's a quick and real neat way of...." type question

Piers Harding piers at ompa.net
Thu Nov 14 23:47:24 CST 2002


Well I'm impressed.

The nicest thing about all this, is it is the most traffic on the list
in ages :-)

Cheers.


On Fri, Nov 15, 2002 at 11:34:20AM +1300, Grant McLean wrote:
> Ewen McNeill wrote"
> > In message 
> > <5FA042F680739D44951B00FED8BE1A7D06A091 at dasher.webdom1.web.co.nz>, "S
> > haun McCarthy" writes:
> > >In which case I think:
> > > 
> > >while (@a && my($a, $b) = (shift @a, shift @b)) {
> > >}
> > 
> > my $len = ((scalar @a) <= (scalar @a) ? (scalar @a) : (scalar @b));
> > for (my $i = 0; $i < $len; ++$i)
> > {
> >   my ($a, $b) = (@a[$i], @b[$i]);
> > }
> 
> I absolutely agree that this is the way I would do it in 
> 'real life' (tm).  That teensy little side affect of both
> arrays being destroyed would usually be a problem.
> 
> I'd think twice before using @a and $a in such close 
> proximity.  An argument could be made that it is actually
> clearer in this case.
> 
> > while ((scalar @a) > 0 && (scalar @b) > 0)
> > {
> >   my ($a, $b) = (shift @a, shift @b);
> > }
> 
> you can avoid using the 'scalar' function by simply using
> its arguments in a scalar or a boolean context:
> 
>   while (@a > 0 and @b > 0) {
> 
> or more simply
> 
>   while (@a and @b) {
> 
> Although in this case 'or' might be better than 'and'.
> 
> Regards
> Grant
> 
> ===============================================================
> Grant McLean        BearingPoint Inc - formerly The Web Limited
> +64 4 495 9026           Level 6, 20 Customhouse Quay, Box 1195
> gmclean at bearingpoint.biz                Wellington, New Zealand



More information about the Wellington-pm mailing list