logo

internet.com
Join the free
WDVL newsletter!

home

Cropping scalar variables with the chop function

Cropping scalar variables with the chop function

  • Sometimes, you do not want the entire value that has been assigned to a scalar variable. For example, it is often the case that the lines you retrieve from a data file will incorporate a new line character at the end of the line. Data files often take advantage of the new line character as a "database row delimiter". That is, every line in a database file is a new database item. For example, here is a snippet from an address book data file:

    Sol|Selena|sol@foobar.com|456-7890 
    Birznieks|Gunther|gunther@foobar.com|456-7899 
    

  • When the script reads each line, it also reads in the new line information. Thus, the first line is actually represented as:

    Sol|Selena|sol@foobar.com|456-7890\n 
    

  • The final "\n" is a new line. Since we do not actually want the "\n" character included with the last database field, we use the chop function. The chop function chops off the very last character of a scalar variable using the syntax:

    chop ($variable_name); 
    

  • Thus, we would take off the final new line character as follows:

    $database_row = "Sol|Selena|sol@foobar.com|456-7890\n"; 
    chop ($database_row); 
    

Using the "." Operator
Table of Contents
Finding the length of a scalar variable with the length function

Up to => Home / Authoring / Scripting / Tutorial

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


internet.com
e-commerce
WebDeveloper Network



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

www.internet.com