On Mon, Apr 05, 2010 at 08:55:39AM -0700, ubuntu wrote:
> Hi
> 
> I am new in perl script. Does anyone know how to implement this puzzle
> in perl?
> 
> [ 0 ]                        =  0
> [ 1 ]                        =  1
> [ 0, 1 ]                     =  1
> [ 1, 1 ]                     =  2
> [ 1, [ 1, 2 ] ]              = -2
> [ 1, 2, [ 8 ] ]              = -5
> [ 1, [ 2 ], [ 3 ] ]          = -4
> [ 9, [ 3, 4, [ [ 2 ] ] ] ]   =  0
> ...
> 
> Thanks

  sub F { my $s; $s += ref() ? -F(@$_) : $_ for @_; $s }

$ echo "[ 9, [ 3, 4, [ [ 2 ] ] ] ]" | perl -le 'sub F { my $s; $s += ref() ?  
-F(@$_) : $_ for @_; $s } print -F(eval<>)'
0

** Warning - no error checking **

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to