|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
Prev | Up | Contents | Down | Next
You can create and initialize arrays in hashes
my @array = (1,3,5,12,37,42);
my %hash = ( alpha => 4, beta => 6 );
Scalars are often used for representing and manipulating strings. Perl provides five classic PL/1-style operations on strings (length, concatenation (denoted as . -dot), substr, index and tr -- translate) and a lot of minor, Unix inpired primitives like chop, chomp, uc, lc.
Perl preserves the possibility of using substr on the left side of assignment statement -- the possibility pioneered in PL/1. A very useful string function is split.
| Function | Description |
|---|---|
| chomp(scalar) chomp(array) | Intelligent chop. By default removes a newline. Can remove any tail that is specified in a special variable $/. Removal is performed only if tail matched $/. Can operate on all elements of an array. |
| chop(scalar) chop(array) | Removes and return the last character from a string. Can operate on arrays. In this case will remove the last character from every element in an array. |
| chr(scalar) | Scalar should a number between 0 and 255. Returns the character that in ASCII code has internal representation (converted to decimal) equal to the NUMBER. For example, chr(65) returns the letter A. |
| crypt(scalar1, scalar2) | Encrypts STRING1. |
| index(string, substring, start_position) | Search for a substring in string. Returns the position of the first occurrence of the substring in string at or after start_position. If you don't specify start_position, it assumed to be 0 (the beginning of the string). See also rindex |
| join(string, array_to_join) | Opposite to the split function.
Concatenate all element of the array and returns the resulting string.
The first argument specifies delimiter to be used in the concatenation.
For example, join('.', ('131', '10', '10')) will return "131.10.10". |
| lc(scalar) | Opposite to uc.
Returns a string with every letter of scalar in lowercase. For example, lc("YAHOO") returns "yahoo". |
| lcfirst(scalar) | Returns a string with the first letter of scalar in lowercase. For example, lcfirst("Yahoo") returns "yahoo". |
| length(scalar) | A classic string operation function. Returns the length of the scalar. Not applicable to arrays and hashes. |
| rindex(scalar, substring, start_position) | Similar to index but return the position of the last occurrence of the substring in string at or after start_position. If you don't specify start_position, it assumed to be 0 (the beginning of the string). |
| split(regex, string, limit) | A very important function. Breaks up a string based on some delimiter (can be a regex). In an array context, it returns a list of substrings that match regex. In a scalar context, returns the number of matches found. |
| substr(string, offset, length) | A very important function. Returns a portion of string starting from the position specified by the offset and length determined by the length parameters. If length is not specified returns tail of the string, starting from the offset position. A negative offset can be used and will count characters from the end of the string. |
| tr/fromset/toset/ | The tr function allows character-by-character translation. Has several important options discussed in the text. |
| uc(string) | Opposite to lc. Returns a string with every letter of the string in uppercase. For example, uc("abba") returns "ABBA". |
| ucfirst(string) | Opposite to lcfirst. Returns a string with the first letter of the string in uppercase. For example, ucfirst("nick") returns "Nick". |
Prev | Up | Contents | Down | Next
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Created: November 7 1998; Last modified: September 05, 2009