aboutsummaryrefslogtreecommitdiff
path: root/challenge-163/luca-ferrari/postgresql/ch-2.plperl
blob: f07bcbc63592ddb3132c886b9dc3008b0ae1dede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- Perl Weekly Challenge 163
-- Task 2

CREATE SCHEMA IF NOT EXISTS pwc163;

CREATE OR REPLACE FUNCTION
pwc163.task2_plperl( int[] )
RETURNS int
AS $CODE$

my ( $n ) = @_;
my @matrix;

push @matrix, [ @$n ];
 while ( scalar( @{ $matrix[ -1 ] } ) > 2 ) {
      my @row;
      push @row, $matrix[ -1 ][ 1 ];
      for my $index ( 2 .. scalar( @{ $matrix[ -1 ] } ) - 1 ) {
          push @row,  $matrix[ -1 ][ $index ] + $row[ -1 ];
      }

      elog( DEBUG, "Current row: " . join( ',', @row ) );
      push @matrix, [ @row ];
 }

return $matrix[ -1 ][ -1 ];
$CODE$
LANGUAGE plperl;