in reply to Re: sprintf and for loop weirds???
in thread sprintf and for loop weirds???

Of course it "should" have read:
for my $n (0..$#a) { ... }
to avoid the possiblity of the off-by-one mistake in the first place. ;-)

Update: or for (@a) as larsen notes below....

-Blake

Replies are listed 'Best First'.
Re: Re: Re: sprintf and for loop weirds???
by rob_au (Abbot) on Nov 04, 2001 at 16:20 UTC
    Or even more robustly with ...

    for my $n ($[..$#a) { ... }

    For the rare possibility where the first index of arrays may be non-zero as a result of some other strange code obfuscation. :-)

    Update : push(@b, sprintf("%.2f", $_)) foreach @a; as suggested by larsen may be nicer and much prettier, but hey! I like obfuscated code :-)

     

    Ooohhh, Rob no beer function well without!

      Looks like the perfect place for map.
      my @b = map {sprintf("%.2f",$_)} @a;

      Oh, you said obfuscated.... hows this for a first step down obfuscated lane:

      my @b = split' ',sprintf" %.2f"x@a,@a;
      one more step, and I'm done:
      s;;sprintf" %.2f"x@a,@a;e;my@b=split;

      -Blake

        Yes, but you still use sprintf ...

        my@b=map{eval{$_=int$_*10**2;s;(..)$;\.$1;;return$_}}@a;

         

        Ooohhh, Rob no beer function well without!