Re: [HACKERS] substring extraction - Mailing list pgsql-hackers

From wieck@debis.com (Jan Wieck)
Subject Re: [HACKERS] substring extraction
Date
Msg-id m11rUgu-0003kJC@orion.SAPserv.Hamburg.dsh.de
Whole thread Raw
In response to Re: [HACKERS] substring extraction  (Karel Zak - Zakkr <zakkr@zf.jcu.cz>)
List 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@debis.com (Jan Wieck) #

pgsql-hackers by date:

Previous
From: "Hiroshi Inoue"
Date:
Subject: RE: [HACKERS] Concurrent VACUUM: first results
Next
From: Tatsuo Ishii
Date:
Subject: Re: