Removing duplicate elements from array

Perl May 25th, 2008

without sorting

my %hash = map { $_, 1 } @array;
@array = keys %hash;

with sorting

my %hash = map { $_, 1 } @array;
@array = keys %hash;
@array = sort(@array); # sorting

with sorting (shorter)

my %hash = map { $_, 1 } @array;
@array = sort(keys %hash);

Tags: ,

Check if a variable is a number

Perl, WordPress May 22nd, 2008

กรณีต้องการเป็นตัวเลข 0-9

1
2
3
4
5
6
if ($var =~ /^\d+$/ ) {
	print "Is a number\n";
}
else {
	print "Is not a number\n";
}

กรณีต้องการให้มี +/- ข้างหน้าตัวเลข (หรือไม่มีก็ได้)

1
2
3
4
5
6
if ($var =~ /^[+-]?\d+$/ ) {
	print "Is a number\n";
}
else {
	print "Is not a number\n";
}

Tags:

HTML escape

Template Toolkit May 6th, 2008

ใช้ FILTER เป็นตัวจัดการ โดย FILTER จะทำการแปลง ‘<’, ‘>’ and ‘&’ ให้แสดงผลได้อย่างถูกต้อง
โดยปกติตัว engine จะ strip tag พวกนี้ไว้ก่อนแล้ว แต่บาง case อาจจำเป็นต้องใช้

{% text | html %}

หรือ

{% text FILTER html %}

text เป็นค่าที่รับจากตัว engine

สมมุติค่าของ text เป็น

HTML text may have < and > characters embedded
which you want converted to the correct HTML entities.

ผมที่ได้จะออกมาเป็น

HTML text may have &lt; and &gt; characters embedded
which you want converted to the correct HTML entities.

Tags: ,

การนำค่า array ไปใช้แสดงผลแบบวนลูป

Template Toolkit May 6th, 2008

syntax ในไฟล์ tt:

{% FOREACH animal IN animals %}
{% animal %}
{% END %}

animals เป็นค่า array ที่ตัว engine ส่งค่ามา

Tags: , ,

Copyright © 2008 iBiz Network Co., Ltd. Powered by wordpress, Theme by ericulous