aboutsummaryrefslogtreecommitdiff
path: root/challenge-184/luca-ferrari/postgresql/ch-2.sql
blob: c1fa0cc3ee7e13efd867aaa960b09cdf37064322 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Perl Weekly Challenge 184
-- Task 2

CREATE SCHEMA IF NOT EXISTS pwc184;

CREATE OR REPLACE FUNCTION
pwc184.task2_plpgsql( strings text[])
RETURNS TABLE (n text, l text)
AS $CODE$
DECLARE
        current_string text;
        current_thing text;
BEGIN
        n := null;
        l := null;
        FOREACH current_string IN ARRAY strings LOOP
                FOREACH current_thing IN ARRAY regexp_split_to_array( current_string, '' ) LOOP
                        -- since '\w' gets also numbers
                        -- the test is performed only if it is not
                        -- a number
                        IF current_thing ~ '\d' THEN
                           IF n IS NULL THEN
                              n := current_thing::text;
                           ELSE
                             n := n || ',' || current_thing;
                           END IF;
                        ELSEIF current_thing ~ '\w' THEN
                           IF l IS NULL THEN
                              l := current_thing::text;
                           ELSE
                             l := l || ',' || current_thing;
                           END IF;
                        END IF;
                END LOOP;
        END LOOP;

        RETURN NEXT;
        RETURN;
END
$CODE$
LANGUAGE plpgsql;