Re: split() function

new topic     » topic index » view thread      » older message » newer message

Be careful what you wish for, you just might get it... without comments, of
course smile


function split(sequence s, integer c)
  sequence result
  integer index, next
  result = {}
  index = 1
  next = find(c, s)
  while next do
   result = append(result, s[index..index+next-2])
   index += next
   next = find(c, s[index..length(s)])
  end while
  result = append(result, s[index..length(s)])
  return result
end function


procedure smart_print_recursive(object o)
 if sequence(o) then
  if equal(repeat(1, length(o)), o >= ' ' and o <= 255) then
   printf(1, "\"%s\"", {o})
  else
   puts(1, "{ ")
   for i = 1 to length(o) do
    smart_print_recursive(o[i])
    if (i < length(o)) then puts(1, ", ") end if
   end for
   puts(1, " }")
  end if
 else
  printf(1, "%g", {o})
 end if
end procedure

procedure smart_print(object o)
 smart_print_recursive(o)
 puts(1, "\n")
end procedure

smart_print( split("|two|three|four|five", '|') )
smart_print( split("one|||five", '|')           )
smart_print( split("one|two|three|four|", '|')  )


-- Pete


On Wed, 3 May 2000 18:57:47 -0400, David Garcia <donovan at ABS.NET> wrote:

>No Solution wrote:
>
>> That's the most more what i had in mind, but i was too lazy to code
>> something like that, and at the time i wrote the function i only needed
to
>> use 1 character so..
>
>I still haven't seen a solution to this which correctly handles the
following
>boundary conditions:
>
>(for these examples, the pipe character is the delimiter, and we expect to
>return a sequence of sequences )
>
>1) null string in front of the first delimiter.
>
>"|two|three|four|five"    should return { {}, "two", "three", "four",
"five" }
>
>2) adjacent delimiters with null string between them.
>
>"one|||five"    should return { "one", {}, {}, {}, "five" }
>
>3) sequence ending in a delimiter.
>
>"one|two|three|four|"    should return { "one", "two", "three", "four", {}
}

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu