Function to return number of words in a string?
Hiya-
I'm looking for a function to return the number of words in a string,
split on whitespace. I'm coming from python, so I may just write it in
that but I wanted to check first. In python, one would write:
s="some string or other"
len(s.split())
Thanks!
--
Peter Fein pfein@pobox.com 773-575-0694
Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
You can use a combination of regex
(http://www.postgresql.org/docs/8.0/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP)
and strpos inside a plpgsql function to do this, but using plpython or
plperl might be faster. A C function would possibly be faster still.
On Mon, May 09, 2005 at 11:21:28AM -0500, Peter Fein wrote:
Hiya-
I'm looking for a function to return the number of words in a string,
split on whitespace. I'm coming from python, so I may just write it in
that but I wanted to check first. In python, one would write:s="some string or other"
len(s.split())Thanks!
--
Peter Fein pfein@pobox.com 773-575-0694Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828
Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"
On Mon, May 09, 2005 at 11:49:41AM -0500, Jim C. Nasby wrote:
You can use a combination of regex
(http://www.postgresql.org/docs/8.0/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP)
and strpos inside a plpgsql function to do this, but using plpython or
plperl might be faster. A C function would possibly be faster still.On Mon, May 09, 2005 at 11:21:28AM -0500, Peter Fein wrote:
Hiya-
I'm looking for a function to return the number of words in a string,
split on whitespace. I'm coming from python, so I may just write it in
that but I wanted to check first. In python, one would write:s="some string or other"
len(s.split())
abacus=> select array_upper(string_to_array('one two three four', ' '), 1);
array_upper
-------------
4
(1 row)
Not that this is a _good_ way of doing it, but it is possible without resorting
to a PL.
Cheers,
Steve
On 05/09/05 11:21 AM CDT, Peter Fein <pfein@pobox.com> said:
Hiya-
I'm looking for a function to return the number of words in a string,
split on whitespace. I'm coming from python, so I may just write it
in that but I wanted to check first. In python, one would write:s="some string or other"
len(s.split())
For the archives:
CREATE OR REPLACE FUNCTION word_length(text)
RETURNS int4 AS
'return len(args[0].split())'
LANGUAGE 'plpythonu' IMMUTABLE STRICT;
Thanks all.
--
Peter Fein pfein@pobox.com 773-575-0694
Basically, if you're not a utopianist, you're a schmuck. -J. Feldman