Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Re: [HACKERS] string_to_array with empty input


  • From: "David E. Wheeler" <david(at)kineticode(dot)com>
  • To: Sam Mason <sam(at)samason(dot)me(dot)uk>
  • Cc: pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
  • Subject: Re: [HACKERS] string_to_array with empty input
  • Date: Tue, 31 Mar 2009 08:56:32 -0700
  • Message-id: <69813E76-A2B1-452B-B5B0-F0B2BC708A64@kineticode.com> <text/plain>

On Mar 31, 2009, at 8:34 AM, Sam Mason wrote:

What do you really expect to be returned for things like

select count_elements(string_to_array('butter,tea,milk',','))

3 = {butter,tea,milk}

select count_elements(string_to_array('butter,tea',','))

2 = {butter,tea}

select count_elements(string_to_array('butter',','))

1 = {butter}

select count_elements(string_to_array('',','))

1 = ARRAY['']

I'd expect 3,2,1 and 1.

That's also a disingenuous example; what would you expect back from:

 select count_elements(string_to_array('butter,,milk',','))

3 = ARRAY['butter', '', 'milk']

I think the semantics you want is what you'd get from:

 array_filter_blanks(string_to_array($1,$2))

where I defined "array_filter_blanks" in my previous post.

Yeah, if I wanted something like that in Perl, I'd do:

  my @stuff = grep { $_ } split /,/, $string;

In no case would I ever expect a NULL, however, unless I was trying to split on NULL.

NULL = string_to_array(NULL, ',');

Best,

David




Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group