From aa4c667437e5674d217fc9f008ad2430a3caece1 Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Mon, 8 May 2023 10:54:32 +0200
Subject: Task 1 plpgsql

---
 challenge-216/luca-ferrari/postgresql/ch-1.sql | 33 ++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 challenge-216/luca-ferrari/postgresql/ch-1.sql

(limited to 'challenge-216/luca-ferrari/postgresql/ch-1.sql')

diff --git a/challenge-216/luca-ferrari/postgresql/ch-1.sql b/challenge-216/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..b929a9a3fb
--- /dev/null
+++ b/challenge-216/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,33 @@
+--
+-- Perl Weekly Challenge 216
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-216/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc216;
+
+CREATE OR REPLACE FUNCTION
+pwc216.task1_plpgsql( rc text, strings text[] )
+RETURNS SETOF TEXT
+AS $CODE$
+DECLARE
+	current_word text;
+	matches int;
+BEGIN
+	FOREACH current_word IN ARRAY strings LOOP
+	    SELECT count(*)
+	    INTO matches
+	    FROM regexp_split_to_table( rc, '' ) r
+	    , regexp_split_to_table( current_word, '' ) w
+	    WHERE r = w;
+
+	    IF matches = length( current_word ) THEN
+	       RETURN NEXT current_word;
+	    END IF;
+	END LOOP;
+
+RETURN;
+END
+$CODE$
+LANGUAGE plpgsql;
-- 
cgit 

