POV-Ray : Newsgroups : povray.programming : strSplit macro. Server Time
7 May 2024 17:02:32 EDT (-0400)
  strSplit macro. (Message 1 to 1 of 1)  
From: jr
Subject: strSplit macro.
Date: 13 Jul 2023 19:20:00
Message: <web.64b08576d08f3556b49d80446cde94f1@news.povray.org>
hi,

included below is a small macro which is part of a currently stalled "pet"
project.  it reads a string containing a list of items, separated by some
character, splits it, and returns an array of the string items.

eg.
#declare list = "one two three";
#declare words = strSplit(list,chr(32));

leaves 'one' in 'words[0]' etc.  an empty list element produces an empty array
element.  hope you will find it useful.


regards, jr.

--------<snip>--------
#macro strSplit(s_,sep_)
  #local n_ = strlen(s_);
  #if (!n_)
    #error "strSplit: string must not be empty."
  #elseif (1 != strlen(sep_))
    #error "strSplit: separator must be a single character."
  #end
  #local a_ = array;
  #local f_ = false;
  #local i_ = 1;
  #local k_ = 0;
  #while (n_ >= i_)
    #if (!strcmp(sep_,substr(s_,i_,1)))
      #if ((i_ - 1) = k_) #local t_ = "";
      #else #local t_ = substr(s_,(k_ + 1),(i_ - k_ - 1));
      #end
      #if (n_ = i_) #local f_ = true; #end
      #local a_[dimension_size(a_,1)] = t_;
      #local k_ = i_;
      #undef t_
    #elseif (n_ = i_)
      #local a_[dimension_size(a_,1)] = substr(s_,(k_ + 1),(i_ - k_));
    #end
    #local i_ = i_ + 1;
  #end
  #if (f_) #local a_[dimension_size(a_,1)] = ""; #end
  a_
#end
--------<snip>--------


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.