Live coverage of Fall Internet World '99! logo

internet.com
Join the free
WDVL newsletter!

home

Using the split and join functions

Using the split and join functions

  • Finally, regular expressions can be used to split a string into separate fields. To do so, we use the "split" function with the format:

    @split_array = split
    (/[pattern_to_split_on]/, [string_to_split]); 
    

  • For example, CGI applications often use the split function to read the fields of database rows. Consider the following code snippet:

    $database_row = "Selena Sol|213-456-7890|27"; 
    @database_fields = split (/\|/, $database_row); 
    

  • Now @database_fields will include the elements "Selena Sol", "213-456-7890" and "27". Each of these fields can then be processed separately if need be.

  • The reverse operation is performed with the "join" function that uses the following format:

    $joined_string = join
    ("[pattern_to_join_on]", [list_to_join]); 
    

  • Thus, we might recreate the original database row using

    $new_database_row = join ("\|", @database_fields); 
    

Notice that in the above line, the pipe (|) symbol must be escaped with a backslash (\) because the pipe is a special Perl character.

Additional Resources:

The =~ operator
Table of Contents
Perl Control Structures

Up to => Home / Authoring / Scripting / Tutorial

Instantly look up a "techie" word using Webopedia!!


internet.com
e-commerce
WebDeveloper Network


Let internet.com work for you!

Copyright 1999 internet.com Corporation. Legal Notices. Privacy Policy.

www.internet.com