Archive for February, 2010
PL/SQL Join Array of Strings
In the last blog post, you learned how to split a string into an “array” of strings by a delimiter such as a comma. In this post, you will create a function that will do the reverse of this operation – take the “array” of strings and join it so that you end up with a single string, tied together by a specified delimiting character. This function will be added to the package that was created in the previous post for convenience. Again, it has been tested on Oracle Database 10g Express Edition. New code in the package specification and body is highlighted for your convenience.
Package specification:
create or replace package emb_string as
type string_array is table of varchar2(32767);
function join_string(str_array in string_array, glue in char default ',') return varchar2;
function split_string(str in varchar2, delimiter in char default ',') return string_array;
end emb_string;
Package body:
create or replace package body emb_string as
function join_string(str_array in string_array, glue in char default ',') return varchar2 is
return_value varchar2(32767);
begin
for i in 1..str_array.count loop
if i = 1 then
return_value := str_array(i);
else
return_value := return_value||glue||str_array(i);
end if;
end loop;
return return_value;
end join_string;
function split_string(str in varchar2, delimiter in char default ',') return string_array is
return_value string_array := string_array();
split_str long default str || delimiter;
i number;
begin
loop
i := instr(split_str, delimiter);
exit when nvl(i,0) = 0;
return_value.extend;
return_value(return_value.count) := trim(substr(split_str, 1, i-1));
split_str := substr(split_str, i + length(delimiter));
end loop;
return return_value;
end split_string;
end emb_string;
Sample Usage 1:
In this example, you create an array of strings with the numbers 1 to 5 using a for loop. You then join this array together as a single string using the default glue character (a comma).
set serveroutput on
declare
v_string_array emb_string.string_array := emb_string.string_array();
begin
for i in 1..5 loop
v_string_array.extend;
v_string_array(i) := i;
end loop;
dbms_output.put_line(emb_string.join_string(v_string_array));
end;
/
Output:
1,2,3,4,5
Sample Usage 2:
In this example, you instantiate the string array with a list of strings in the declare block. You then use the join_string function to join each element to each other using the specified glue character, colon.
set serveroutput on
declare
v_string_array emb_string.string_array := emb_string.string_array('hello','world','how','are','you');
begin
dbms_output.put_line(emb_string.join_string(v_string_array, ':'));
end;
/
Output:
hello:world:how:are:you
PL/SQL Split Strings
In software development, there is often a need to split a string into multiple segments based on a delimiter. This functionality is not provided out of the box with PL/SQL, but fortunately it is relatively easy to implement. The following code has been tested on Oracle Database 10g Express Edition.
Package specification:
create or replace package emb_string as type string_array is table of varchar2(32767); function split_string(str in varchar2, delimiter in char default ',') return string_array; end emb_string;
Package body:
create or replace package body emb_string as function split_string(str in varchar2, delimiter in char default ',') return string_array is return_value string_array := string_array(); split_str long default str || delimiter; i number; begin loop i := instr(split_str, delimiter); exit when nvl(i,0) = 0; return_value.extend; return_value(return_value.count) := trim(substr(split_str, 1, i-1)); split_str := substr(split_str, i + length(delimiter)); end loop; return return_value; end split_string; end emb_string;
Sample Usage 1:
In this example, you split a string using the default delimiter character, the comma.
set serveroutput on
declare
v_split_string emb_string.string_array;
begin
v_split_string := emb_string.split_string('hello,world,how,are,you');
if v_split_string.count > 0 then
for i in 1..v_split_string.count loop
dbms_output.put_line(v_split_string(i));
end loop;
end if;
end;
/
Output:
hello world how are you
Sample Usage 2:
In this example, you split a string by specifying the semi-colon character as the delimiter.
set serveroutput on
declare
v_split_string emb_string.string_array;
begin
v_split_string := emb_string.split_string('very;good;thank;you;very;much', ';');
if v_split_string.count > 0 then
for i in 1..v_split_string.count loop
dbms_output.put_line(v_split_string(i));
end loop;
end if;
end;
/
Output:
very good thank you very much
Sample Usage 3:
This function does not provide a means by which you can “escape” the delimiter character. The easiest way to get around this is to use an obscure delimiter character in your data. In this example, you split a string by the character sequence ~*. You’ll notice that the string does not split when it encounters the tilde or asterisk character alone, but only when they are together in sequence.
set serveroutput on
declare
v_split_string emb_string.string_array;
begin
v_split_string := emb_string.split_string('this~*is~*an~other~*exa*mple~*', '~*');
if v_split_string.count > 0 then
for i in 1..v_split_string.count loop
dbms_output.put_line(v_split_string(i));
end loop;
end if;
end;
/
Output:
this is an~other exa*mple
Compare JavaScript Frameworks
Modern Web sites and Web applications tend to rely quite heavily on client-side JavaScript to provide rich interactivity, particularly through the advent of asynchronous HTTP requests that do not require page refreshes to return data or responses from a server-side script or database system. In this article, you will discover how JavaScript frameworks make it easier and faster to create highly interactive and responsive Web sites and Web applications.
JavaScript is an object-oriented scripting language that has long been the client-side scripting interface of choice for Web browser applications. JavaScript lets Web developers programmatically work with objects on a Web page, providing a platform for manipulating these objects on-the-fly. When JavaScript was first introduced, it was commonly used to provide trivial features on Web pages such as clocks and scrolling text in the browser status bar. Another common feature was the “rollover link,” where an image or text color of a link would be changed when the user rolled their mouse over it. In recent times, however, JavaScript has evolved to become far more useful, with the concept of Asynchronous JavaScript and XML (Ajax) bringing a whole new level of interactivity to Web-based programming. Prior to Ajax, any server-side processing or database access would require the entire page to be “refreshed” or a new page to be rendered by the browser. Not only is this slow and frustrating for the user, but it is also a waste of bandwidth and resources.
Read the article at http://www.ibm.com/developerworks/web/library/wa-jsframeworks/