Re: [HACKERS] substring extraction

From: wieck(at)debis(dot)com (Jan Wieck)
To: zakkr(at)zf(dot)jcu(dot)cz (Karel Zak - Zakkr)
Cc: jose(at)sferacarta(dot)com, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] substring extraction
Date: 1999-11-26 23:22:12
Message-ID: m11rUgu-0003kJC@orion.SAPserv.Hamburg.dsh.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Karel Zak wrote:

> On Fri, 26 Nov 1999, jose soares wrote:
>
> > Try this:
> >
> > --returns the $2 field delimited by $3
> > drop function field(text,int,text);
> > create function field(text,int,text) returns text as
> > 'declare
> > string text;
> > pos int2:= 0;
> > pos1 int2:= 0;
> > times int2:= 0;
> > totpos int2:= 0;
> > begin
> > times:= $2 - 1;
> > string:= $1;
> > while totpos < times loop
> > string:= substr(string,pos+1);
> > pos:= strpos(string,$3);
> > totpos:= totpos + 1;
> > end loop;
> > string:= substr(string,pos+1);
> > pos1:= strpos(string,$3);
> > return substr(string,1,pos1 - 1);
> > end;
> > ' language 'plpgsql';
> >
>
> Oh, it is great! But my implementation in C for this is
> a little longer (only) :-)
>
> I send this question to the hacker list because "extract delimited
> substring" is not a abnormal uses's request, and (IMHO) will very
> good if this will in PgSQL. How much uses known write this in
> C or any PL?

What about this one:

create function field(text,int,text) returns text as '
return [lindex [split $1 $3] $2]
' language 'pltcl';

It does all the work as long as the third argument is a
single character. For multibyte delimiters it will be
slightly bigger, but not much. Now you might imagine why
PL/Tcl was the first language I created.

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#========================================= wieck(at)debis(dot)com (Jan Wieck) #

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tatsuo Ishii 1999-11-27 02:05:31 Re:
Previous Message Hiroshi Inoue 1999-11-26 22:48:05 RE: [HACKERS] Concurrent VACUUM: first results