.
Developer Spot - Web Development Tutorials
arrowDeverloper Spot  Tutorials  PHP  Choosing The Right Server-Side Scripting Language 
 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Web Development News
PHP Manual
Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Choosing The Right Server-Side Scripting Language

By Craig McElwee
2004-01-06
Reader Rating: 4 out of 5
Bookmark Print Version
Task 6: Split Comma-Delimited Line Into Variables

Your script reads a line from a CSV file. How easily does it separate each field into individual variables, in this case, a, b, c, and d? Perl, PHP, and Python all have a handy 'split' function, taking the string to be split and the delimiter used for splitting as arguments. Java servlets and Tcl require you to set each field individually. While fine for this example, this latter approach would be prohibitive if each line had a large number of fields.

Perl:


($a,$b,$c,$d) = split /,/, $lines[@lines-1];

PHP:


list( $a,$b,$c,$d ) = split( ",", $last, 4 );

Python:


a,b,c,d = splitfields(lines[-1], ',')

Java:


String a, b, c, d;

StringTokenizer st = new StringTokenizer(last, ",");
a = st.nextToken();
b = st.nextToken();
c = st.nextToken();
d = st.nextToken();


Tcl:


set fieldlist [split $last ,]
set a [lindex $fieldlist 0]
set b [lindex $fieldlist 1]
set c [lindex $fieldlist 2]
set d [lindex $fieldlist 3]




Article Pages:
How Five Languages Do The Same Basic Tasks
Task 1: Get and Format The Time/Date
Task 2: Put Form Field Data Into Variables
Task 3: Search and Replace
Task 4: File Writing
Task 5: File Reading
Task 6: Split Comma-Delimited Line Into Variables
So, Which One Should You Use?
Resources

First published by IBM developerWorks


 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 4

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics



 
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML
More Resources: Web Hosting Articles - Web Development News - PHP Manual