Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers