Types of Variable

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

Scalars:Holds one number (integer and real) or string value at a time. Scalar variable names always begin with a $.

Arrays:Holds a list of values. The values can be numbers, strings, or even another array. Array, variable names always begin with an @.

Associative Arrays:Uses any value as an index into an array Associative array variable names always beg with an %.

The different beginning characters help you understand how a variable is used when you look at someone else's Perl code. If you see a variable called @Value,you automatically know that it is an array variable.

They also provide a different namespace for each variable type. Namespace, separate one set of names from another. Thus, Perl can keep track of scale variables in one table of names and array variables in another. This lets you us$name, @name, and %name to refer to different values.

Scalar variables are used to track single pieces of information. You would use them to hold the title of a book or the number of rooms in a house. You can use just about any name imaginable for a scalar variable as long as it begins with a $, Most programmers try to use descriptive names for their variables. There is no practical limit to the length of a Perl variable name, but it would be better to keep them under 15 characters, Anything longer than that means that it will take a while to type them and increases the chances of spelling errors,

Assigning Values to Scalar Variables

Now that you know what scalar variable names look like, well took at how you can assign a value to them. Assigning values to a variable is done with the equals (=) sign.
$numberOfRooms = 23;
$degree = 56.45;
$book Title= "Perl by Example";
Notice that you are assigning literal values to the variables. After assigning the values, you then can change them.

Double Quoted Strings

Variable interpolation Scalar and array variables embedded in double quoteu strings are replaced by their values. Thus you can write
$varl = "Pentasoft ";
Svar2 = "$varl is a training institute
print Svar2,
The output will be:
Pentasoft is a training institute

In double quoted strings, characters with a backslash prefix are replaced by a single sepecial character. For example, \n becomes a newline. Perl displays a single quoted string verbatim without replacing variable with corresponding values. You might use single quotes to print string contain single characters, such as $ and @. For example, print $num copies of books'; will be printed as num copies of books

Third type of string quoting is useful when you want to display the output operating system command. Suppose that you want to store the output UNIX Is command ( Is command lists the list of files in the current direct a scalar variable named $out. You can do so by using the following Perl start $out = 'Is';

Getting Values in Scalar Variables

The example will make a variable assignment and then change that variable value using a second assignment. The second assignment will increment value by five. $numberOf Rooms = 23; $numberOf Rooms = $numberOf Rooms + 5;

Array Variables

Array variable names always begin with an 0 character. The rules for array variables are the same as those for scalar variables. Assigning Values to Array Variables You use the equals (=) sign to assign values to array variables just like values. OemptyArray=();
@numberArray = 0 2, 014, OxOc, 34.34, 23.3E 3);
@stringArray ("This", "is", 'an', "array", 'of', "strings");
@mixedArray ("This", 30, "is", 'a', "mixed array", 'of', OxO8, "items print "Here is an empty array:" .
@emptyArray . '< Nothing there!\n"; print
@numberArray; print 'An print @stringArray; print 'An ";

These are definitely the most complicated of the three data types. And YE are just another type of array, With associative arrays you can use any data type as an index. Associative array names start with the % charact You will see associative arrays called hashes at times. The term "hash" to how associative array elements are stored in memory. Assigning Values to Associative Array Variables %associativeArray = ("Jack A.", "Dec 2", "Joe B." "June 2", "Jane C.", "Feb 13"); $associativeArray{"Jennifer S.") = "MaT 20"', print "Joe's birthday is: " . $associativeArray(" Joe B.") . 'An"; print "Jennifer's birthday is: " . $associativeArray("Jennifer S.") . "\n"; This program will print the following: Joe's birthday is: June 2 Jennifer's birthday is: Mar 20

perl with extend the associative array as needed when you assign values to keys. An internal table is used to keep of which keys are defined. If you try to access an undefined key, Perl will return a null or blank string.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.