#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Pl/pgsql function to count number of same words in two strings


    hello forum! could please anyone help me to write a pl/pgsql function to count number of same words in two given strings?
    I've given up here:

    CREATE OR REPLACE FUNCTION word_match (
    1ststr text, 2ndstr text
    ) RETURNS integer AS $$
    BEGIN
    a=string_2_array(1ststr, ' ')
    b=2ndstr
    n=1
    FOR n<array_upper(a) LOOP
    a[n] ilike '%||b||%'

    Than my friend told me that probably it is better to do it through regex like this:
    select count(*) from regexp_split_to_table( 'string one', ' ') a where a in (select regexp_split_to_table( 'string two', ' '));

    but just for the matter of interest could anyone tell how to do it through pl/pgsql?

    thanks a lot! :)))
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0
    Your friend's solution seems perfectly valid and simple. Just throw it into a plpgsql function...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    yeah thanks! in the end i've just wrote pl/pgsql function based on his suggestion...just wondered probably there is some another way, that doesn't use regex functions....
    st_bringer

IMN logo majestic logo threadwatch logo seochat tools logo