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

Grant McLean Grantm at web.co.nz
Thu Nov 14 16:34:20 CST 2002


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