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

Re: [FWP] fwp-digest V1 #109 -Reply



Ariel Scolnicov <ariels@compugen.co.il> writes:
> Here's another 50-char solution.  The double use of map for side effect is
> great Perl style, but that isn't why -w prints the warning...
> 
> $#='%3g';map{$-=$_;print((map{$_*$-}1..9),$/)}1..9

Well, I'm not sure I'd call it `great Perl style', but it's certainly Fun.

> Stealing some ideas from Aaron, we'd get these 50 characters, and no -w
> warning.
> 
> for(1..9){$-=$_;printf'%3d',$_*$-for 1..9;print$/}

I think this is the same as mine except for the quote character and the name
of the variable...

> But obviously this can be shortened!  This is just 46 characters (&
> still no warning).
> 
> for$-(1..9){printf'%3d',$_*$-for 1..9;print$/}

...but I wish I'd seen this one -- it's the natural progression from mine.

(By the way, I feel a little weird seeing the $- there.  Maybe it's just me,
but those format-related variables don't feel like they're mine.  I prefer
to use things like $, and $" when I'm stealing punctuation variables -- it's
definitely the user's prerogative to munge them at will.  Still, perlvar
doesn't list them as read-only, so I suppose they're available.  Perhaps
it's partly that I don't actually use formats much.)

As an aside, more in response to the thread on eliminating whitespace than
to your article, I'd like to speak just briefly about what makes these Fun.
Part of the interest for me is the observation that when you write
minimal-character-count code you tend to get caught up in what you're doing
-- it's fairly hard to step outside what you're doing and find a better (or
in this case, shorter) Way To Do It.  Take John Porter's code that I
shortened:

    do{$j=$_;formline('@>>',$_*$j)for 1..9;$^A.="\n"}for 1..9;print$^A

I'm guessing that John has spent quite some time observing that `for' as a
statement modifier tends to shrink code, and that for this reason, it can be
useful in one-liners.  But I'm further guessing that John's familiarity with
this construction prevented him from noticing that do{block}for is actually
longer than the simple for{block}.  Likewise, I took the structure of John's
code (including the assignment in the outer loop) and tweaked it only
slightly.  But I was too involved in what I was doing to be able to spot
that making the punctuation variable the loop-control variable in the outer
loop saves that entire assignment.

-- 
Aaron Crane   <aaron.crane@pobox.com>   <URL:http://pobox.com/~aaronc/>

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe