Site hosted by Angelfire.com: Build your free website today!
# Functions
# SPLIT, INDEX, SUBSTR all functions have their syntax and an example
# By Navic, 10-29-01
 
 

my($password);
my($var);
my($str);
my($lngth_pwd);

##########################################################################################
# length($VAR)
# LENGTH function returns the length of a string variable.

$password = "qwerty";
$lngth_pwd = length($password);

##########################################################################################
# split(/PATTERN/, SCALAR, MAX TOKENS)
# SPLIT function splits up a variable looking for the deliminator specified
#
# Arrays have what's called an index. This index indicates the number of
# elements that are contained in the array. To get this number, you can change
# the @ in the array name, to $# which will return the number of elements in the array.

$password = "q w e r t y";
@array = split(/ /, $password);
print $#array, "\n";
##########################################################################################
# index (STR, SUBSTR, POSITION)
# INDEX function looks for the position of a deliminator in the string variable
# Position starts with 0; but the return value is the position where the NEXT deliminator
# shows up, so if you know position 2 is a deliminator, the index function will return 4
# b/c the next deliminator that comes up from position 2 in the variable is in position 4

$password = "q w e r t y";
$first = index($password, " ", 0);
print "First occurence of a space at position: ", $first, "\n";
##########################################################################################
# substr(SCALAR, OFFSET, LENGTH, REPLACEMENT)
# SUBSTR function looks for a substring in a string variable.  In the example below, substr
# starts at position 0, and takes the first 3 characters of the variable $var, and returns
# then to the variable $str.  When runned, $str prints out to "pas" the first 3 characters
# in the $var string (password).  You can set substr to start at any position and look for
# any number of characters in a variable.

$var = "password";

$str = substr($var, 0, 3);
print $var, "\n";
print $str, "\n";
##########################################################################################
# splice (VAR, POSITION, ELEMENTS)
# SPLICE is commonly used with arrays.  The VAR is the array name, POSITION is the posistion
# to start the splice (starts with 0), and ELEMENTS is the number of element you want to
# delete.