Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Sunday, December 19, 2010

javascript中array方法调用返回window对象

array中的很多方法通过call和apply调用时会返回window对象,如下写法在Firefox、Chrome等浏览器中会取到window对象:


window === ([]).sort.call();
window === ([]).reverse.call();
([]).concat.call()[0] === window


可以将这些array的方法重写,避免它在运行时的this指向window,如重写sort方法:

Array.prototype.sort = (function(sort) { return function(callback) {
return (this == window) ? null : (callback ? sort.call(this, function(a,b) {
return callback(a,b)}) : sort.call(this));
}})(Array.prototype.sort);

Tuesday, September 25, 2007

Perl内置数据类型

一、标量scalar是 Perl 中最简单的数据类型。大多数的标量是数字(如 255 或 3.25e20)或者字符串("Hello World!")
Perl 不同于其它的一些语言 ,它没有 Boolean 类型。它利用如下几条规则:
1. 如果值为数字,0 是 false;其余为真
2. 如果值为字符串,则空串( ‘’)为 false;其余为真
3. 如果值的类型既不是数字又不是字符串,则将其转换为数字或字符串后再利用上述规则
4. 这些规则中有一个特殊的地方。由于字符串‘0’和数字 0 有相同的标量值,Perl 将它们相同看待。也就是说字符串‘0’是唯一
一个非空但值为 0 的串

二、列表list是标量的有序集。数组是包含列表的变量。在 Perl 中这个两个术语是可以互换的。但严格意义上讲,列表是指数据, 而数组是其变量名。可以有一些值(列表)但不属于数组;但每一个数组标量都有一个列表,虽然其可以为空。
列表中每一个元素都是一个独立的标量值。这些值是有顺序的,也就是说,这些值从开头到最后一个元素有一个固定的序列。 数组或者列表中的元素是编了号的,其索引从整数 0 开始,依次增一,因此数组或者列表第一个元素的索引为 0。
数组是由括号括起来并且其元素由逗号分隔开的列表。这些值组成了数组的元素: (1,2 ,3) # 含有 1 ,2,3 的列表。

三、哈希hash是一种数据结构,和数组类似,可以将值存放到其中,或者从中取回值。但是,和数组不同的是,其索引不是数字而是任意的唯一的字符串,称作key。

Sunday, September 23, 2007

Perl中对@array和%hash的排序


#! /usr/bin/perl -w
use strict;

# 如果在最终结果中$a 出现在$b 之前,则其排序子程序返回-1。如果$b 出现在$a 之前,则返回 1。
# 如果$a 和$b 的顺序无关紧要,则子程序返回 0。为什么它无关紧要呢?也许你正在做一个大小写无关的排序,而这两个字
# 符串是 fred 和 Fred 。也许你正在做一个数字排序,而这两个元素相等。
sub by_number {
if ($a > $b) {-1} elsif ($a < $b) {1} else {0}
}

my @nums = (1, 6, 2, 7, 3, 8, 4, 9, 5);
@nums = sort by_number @nums;
print "@nums\n";

# 针对本例,我们使用太空船(spaceship)符号(<=>)。这个操作符比较两个数字,按照数字将其排序,并返回-1, 0, 1。
sub by_numerically { $a <=> $b };
@nums = sort by_numerically @nums;
print "@nums\n";

sub case_insenstive { "\L$a" cmp "\L$b"};
print sort case_insenstive ('Last ', 'First ', 'second ', 'third ');
print "\n\n";

my %score = (
"barney" => 195,
"fred" => 205,
"dino" => 30,
"bam-bamm" => 195,
);

sub by_score_and_name {
$score{$a} <=> $score{$b}
or
$a cmp $b;
}
# or的优先级低,在前面太空船'<=>'比较结果后,如果返回的为0,则计算后面的比较。
my @winners = sort by_score_and_name keys %score;
print "@winners\n";

Array的排序方法sort与Javascript, Ruby基本一致。Javascript sort调用方法举例如下:

function by_number(a, b) {
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
}

function by_number_reverse(a, b) {
if (a > b) {
return -1;
} else if (a < b) {
return 1;
} else {
return 0;
}
}
var myArray = [2, 4, 2, 17, 50, 8];
alert( myArray.sort() );
alert( myArray.sort(by_number));
alert( myArray.sort(by_number_reverse));

Sunday, March 25, 2007

variable variables

将某变量值作为变量名时需要注意在结合array使用时的一个问题:
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Converting to object(String,Array)

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built in class is created. If the value was NULL, the new instance will be empty. Array converts to an object with properties named by array keys and with corresponding values. For any other value, a member variable named scalar will contain the value.


<br/>
<?php
$bar = "boo";
$obj = (object) $bar;
var_dump($obj);// object(stdClass)#1 (1) { ["scalar"]=> string(3) "boo" }
echo "<br/>";

$arr = array('x' => 1, 'y' => 2, 'z' => 3);

$obj_arr = (object) $arr;
var_dump($arr); //array(3) { ["x"]=> int(1) ["y"]=> int(2) ["z"]=> int(3) }
var_dump($obj_arr); //object(stdClass)#2 (3) { ["x"]=> int(1) ["y"]=> int(2) ["z"]=> int(3) }
echo "<br/>";

$var = 'x';
echo $obj_arr->x; // 1
echo "<br/>";
echo $obj_arr->$var; // 1
?>

Friday, March 16, 2007

Array Search Prototype

from DZone Snippets: javascript code by pcx99 (Patrick)
This prototype extends the Array object to allow for searches
within the Array. It will return false if nothing is found. If
item(s) are found you'll get an array of indexes back which matched
your search request. It accepts strings, numbers, and regular expressions as search
criteria. 35 is different than '35' and vice-versa.

// Examples
var test=[1,58,'blue','baby','boy','cat',35,'35',18,18,104]
result1=test.find(35); //returns 6
result2=test.find(/^b/i); //returns 2,3,4
result3=test.find('35'); //returns 7
result4=test.find(18); // returns 8,9
result5=test.find('zebra'); //returns false



Array.prototype.find = function(searchStr) {
var returnArray = false;
for (i=0; i if (typeof(searchStr) == 'function') {
if (searchStr.test(this[i])) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
} else {
if (this[i]===searchStr) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
}
}
return returnArray;
}