Wednesday, June 27, 2007

perl function

############
# code : Array
@book=("PHP","Perl");
@morebook=("database","internet");
@morebook=("database","internet",@book);
# Output: @morebook=("database","internet","PHP","Perl");

############
# Code: scalar(@arr)
# This function forces an array or hash to be interpreted as a scalar.
# This is useful to find out the number of elements in an array or hash.
# Use this function to find out the size of an array or hash, 
# preparatory to iterating over it in a loop and processing its contents.
# define array
@data = ('apple', 'peach', 'banana');
# get size
print "The array has " . scalar(@data) . " elements";
# Output: The array has 3 elements


############
# Code: exists $hash{$key}
# This function can be used to test if a particular key exists in a hash.
# define hash
%data = ('king' => 'queen', 'prince' => 'princess');
# check if key exists
if (exists $data{'king'}) {
  print "Found!";
}
# Output: Found!


############
# Code: push(@arr, $val)
# push จะเพิ่มค่าตัวแปรให้กับตัวแปรตัวสุดท้าย
# define array
@data = ('a', 'b', 'c');
# add element
push(@data, 'd');
print "@data ";
# Output: a b c d


############
# Code: pop(@arr)
# define array
@data = ('a', 'b', 'c');
# add element
pop(@data);
print "@data ";
# Output: a b


############
# Code: unshift(@arr, $val)
# This function adds a new element to the beginning of an array.
# define array
@data = ('a', 'b', 'c');
# add element
unshift(@data, 'z');
print "@data ";
# Output: z a b c


############
# Code: shift(@arr)
# This function removes an element from the beginning of the array.
# define array
@data = ('a', 'b', 'c');
# remove element
shift(@data);
print "@data ";
# Output: b c


############
# Code: splice(@arr, $offset, $length, @arr2)
# This function removes a segment of an array, starting from the element at position $offset 
# and continuing until $length elements have been removed. If the optional @arr2 argument is present,
# the extracted segment is replaced with the element of array @arr2.
# Use this function to extract subsets of an array, or to replace elements in an array with new values.
# define array
@data = ('king', 'queen', 'knight', 'bishop');
# remove middle elements
splice(@data, 1, 2);
print "@data ";
# Output: king bishop


############
# Code: delete $hash{$key}
# This function deletes a key from a hash, together with its associated value.
# Use this function to remove elements from a hash. 
# define hash
%data = ('king' => 'queen', 'prince' => 'princess');
# remove element with key 'king'
delete $data{'king'};


############
# Code: split($delim, $str)
# This function decomposes a string by splitting it on delimited $delim and returns the individual 
# components as elements of a numerically-indexed array. These elements can then be processed in a loop.
# Use this function to split comma-delimited lists into independent array elements.
# define string
$str = "cat,hog,dog,log";
# split string on comma delimiter
@words = split(",", $str);
foreach $w (@words) {
  print "$w\n";
}
#Output:
#cat
#hog
#dog
#log


############
# Code: join($sep, @arr)
# This function joins the various elements of an array into a single string, 
# using the value of $sep to separate them from each other.
# Use this function to create a single string from multiple independent array elements,
# using spaces, commas or other separators to glue them together.
# define array
@data = ("Harry", "Joan", "Tom");
# create string from array
print join(" and ", @data) . " are friends";
# Output: Harry and Joan and Tom are friends


############
# Code: keys(%hash)
# This function returns the keys of a hash as a numerically-indexed array. 
# The function is the counterpart of the values() function, discussed next.
# Use this function to extract the keys of a hash into a separate data structure for further processing.
# define hash
%data = ('a' => 'apple', 'b' => 'bat', 'c' => 'cat');
# get and print hash keys
@keys = keys(%data);
print "@keys\n";
# Output: c a b


############
# Code: values(%hash)
# This function returns the values of a hash as a numerically-indexed array.
# The function is the counterpart of the keys() function, discussed previously.
# Use this function to extract the values of a hash into a separate data structure for further processing.
# define hash
%data = ('a' => 'apple', 'b' => 'bat', 'c' => 'cat');
# get and print hash keys
@vals = values(%data);
foreach $v (@vals) {
  print "$v ";
}
# Output: cat apple bat


############
# Code: reverse(@arr)
# This function reverses the order of elements of an array, placing the last element first and vice-versa.
# Use this function to re-sort an array's elements in the opposite direction to their current sort.
# define array
@data = ('apple', 'peach', 'banana');
# reverse array
@rev = reverse(@data);
print "@rev ";
# Output: banana peach apple


############
# Code: sort(@arr)
# This function can be used to sort the elements of an array or hash. By default,
# this function sorts using standard string comparison rules; however,
# you can override this by passing it the name of a custom sorting subroutine.
# Use this function to reset the internal order of elements of an array,
# or to arrange elements alphabetically, numerically or in a custom order.
# define hash
@data = ('oranges', 'peaches', 'grapes', 'apples', 'lemons');
# sort alphabetically
@sorted = sort(@data);
print "@sorted ";
# Output: apples grapes lemons oranges peaches


############
# Code: foreach
@myNames = ('Larry', 'Curly', 'Moe');
foreach (@myNames) {
  print $_;
}
# Output: LarryCurlyMoe


############
# Code: foreach
@myNames = ('Larry', 'Curly', 'Moe');
foreach $name (@myNames) {
  print $name;
}
# Output: LarryCurlyMoe


############
# Code: qw
# quote word in Perl
@myNames1 = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Alexander', 'Andrew');
@myNames2 = qw(Jacob Michael Joshua Matthew Alexander Andrew);
@myNames3 = qw/Jacob Michael Joshua Matthew Alexander Andrew/;
print "\@myNames1 = @myNames1\n";
print "\@myNames2 = @myNames2\n";
print "\@myNames3 = @myNames3\n";
# Output: 
# @myNames1 = Jacob Michael Joshua Matthew Alexander Andrew
# @myNames2 = Jacob Michael Joshua Matthew Alexander Andrew
# @myNames3 = Jacob Michael Joshua Matthew Alexander Andrew


############
# Code: Single and Double Quoted Strings
$my_name = 'Bobby';
print "$my_name\n";
print '$my_name\n';
# Output: 
# Bobby
# $my_name\n

1 comment:

dat ha said...



Tiểu cô nương một lần nữa đánh về phía Nhạc Thành, cùng lúc đó, hồng mang lay động, ngưng tụ thành một mảng long trời lở đất.

- Nhạc Thành, cẩn thận.

Yêu Huyên nhìn thấy Nhạc Thành gặp phải đối thủ mạnh mẽ như thế, mặc dù mình không biết phải giúp gì nhưng vẫn lo lắng cho Nhạc Thành, một mảng quang mang màu lục bắn lên bầu trn Nhãn Nhiếp Hồn.
dongtam
mu private
tim phong tro
http://nhatroso.com/
nhac san cuc manh
tổng đài tư vấn luật
http://dichvu.tuvanphapluattructuyen.com/
văn phòng luật
tổng đài tư vấn luật
dịch vụ thành lập công ty
http://we-cooking.com/
chém gió
trung tâm ngoại ngữ
Yến Hiểu Kỳ lúc này cũng lo lắng cho Nhạc Thành, nàng nhìn thấy Yêu Huyên phát động công kích thì cũng không cam lòng ở phía sau, bắt đầu sử dụng thiên phú mạnh nhất của Vũ Văn gia chính là Thiên Nhãn Nhiếp Hồn.

- Bát Sí linh mãng, Thiên Nhãn Nhiếp Hồn của Vũ Văn gia.

Nhìn thấy bản thể của Yêu Huyên và công kích của Yến Hiểu Kỳ, Bàn Thiên Lão Ma vẫn chưa hết khiếp sợ, bọn họ tới giờ mới được biết thân phận của Yến Hiểu Kỳ và Yêu Huyên.

Why You Don't LIKE My FaceBook Fanpage ?
×
blogger