Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Variables and Data Types

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

casien
Newbie


Posts: 8
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 0 (Donate)
Post: #1

Variables and Data Types


Scalar Variables

Scalar variables can be a number, a string or undefined. Scalar varaibles are prefixed by $ sign. Here are some scalar variable example:

$str1="www"; #assign string "www" to scalar variable $str1
$str2="cgi"; #assign string "cgi" to scalar variable $str2
$str3=$str1.$str2; #concatenate two strings so $str3="wwwcgi"
$int=5; #assign integer 5 to scalar variable $int
$int=3+2; #here $int=5
$int=3*4; #here $int=12
$int=5;$int++; #here $int=5+1=6
$int=5;$int+=4; #here $int=5+4=9

Array of Scalar

Array of scalar should be prefixed by @ sign. But when you specify one element in an array, you can also prefix it with a $ sign. Here are some array of scalar examples:

@name1=("viky","jeff"); #assign "viky","jeff" two strings to array @name1
@name2=@name1; #now @name2=("viky","jeff")
@name3=("john",@name1); #now @name3=("john","viky","jeff")
($one,@name4)=@name3; #now @one="john" ,@name4=("viky","jeff")
@name1=(); #now @name1 becomes an empty array
@int=(2,6,7,8,9); #Five numbers are assigned to array @int
$x=@int; #assign an array to an scalar variable will return the
#number of elements in the array, so $x=5
$x=$#int; # $# is a special variable which will return the index of the
last element in an array, so $x=4
($x)=@int #$x is equal to the first element of array, so $x=2
$b=$int[0]; #$b is equal to the first element in array, so $b=2
$c=@int[0]; #$c is also equal to the first element in array, so $c=2
# $int[0] works the same way as @int[0]
$int[0]=5; #assign 5 to be the first element of array, so @int=(5,6,7,8,9)
$int[0,1]=[1,3] #asisgn 1 to the first element, 3 to be the second element, so @int=(1,3,7,8,9)
@int[0,1]=@int[1,0] #the first two element exchange ,now @int=(3,1,7,8,9)
@data=@int[0,1] #here @data=(1,3)
$int[5]=10 # assigns the sixth element to @int so @int=(1,3,7,8,9,10)

Associate Array

Associative arrays, sometimes called "hashes", are arrays that are indexed not on ordered integers, but on arbitrary string values. Typically, elements of an associative array are referred to as "key" and "value" pairs - the "key" is used to find the element of the array that has the "value". An associate array is prefixed with the percent(%) sign. Here is the format for an associate array:

%arrayname=(key1,value1,key2,value2,key3,value3...);

For each key in asscoiate array, there is a value corresponding to it:

* Add or change a pair of element:

$arrayname{key}=value

To add a pair of key-value to associate array, first prefix the array name with dollar sign($), and embrace the key inside the {} then assign the value to the key. If the key is already in this array, then the key's value will change.
* Get the the value of a key :

$value=$arrayname{key}

* To delete a pair of key-value use the following statement:

delete $arrayname{key}

where the delete is a function provided by Perl.

Here are some associate array examples:

%name=('john','biology','jeff','physics'); # suppose john teaches biology and jeff teaches physics
$a=$name{'john'}; # here $a='Biology'
$b=$name{'jeff'}; # here $b='physics'
$name{'bill'}='math'; # add a new pair of element : key is bill, its value is math.
# now %name=('john','biology','jeff','physics','bill','math')
$name{'bill'}='English'; # Because we have already have a key 'bill' in the associate array,
# so the 'bill' value change to 'English'
# now %name=('john','biology','jeff','physics','bill','English')
delete $name{'jeff'}; # now #now %name=('john','biology','bill','English')
@X=%name # associate array assign to array @X
%name=@X # array assign to associate array.

29-09-2006 06:55 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  CGI Environment Variables casien 0 626 29-09-2006 06:28 AM
Last Post: casien

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 08-10-2008, 01:33 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB