containers
スクリプト言語の比較
http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。
containers
perl | php | python | ruby | |
---|---|---|---|---|
array literal |
@nums = (1,2,3,4);
|
<?php $nums = array(1,2,3,4); ?>
|
nums = [1,2,3,4]
|
nums = [1,2,3,4]
|
array size |
$#nums + 1 or scalar(@nums)
|
<?php count($nums) ?>
|
len(nums)
|
nums.size
nums.length # same as size
|
array lookup |
$nums[0]
|
<?php $nums[0] ?>
|
nums[0]
|
nums[0]
|
array slice |
@nums[1..2]
|
<?php # 3rd arg is length of slice: array_slice($nums,1,2) ?>
|
nums[1:3]
|
nums[1..2]
|
manipulate back of array |
@a = (6,7,8); push @a, 9; pop @a;
|
<?php $a = array(6,7,8); array_push($a, 9); array_pop($a); ?>
|
a = [6,7,8] a.append(9) a.pop()
|
a = [6,7,8] a.push(9) a << 9 # same as push a.pop
|
manipulate front of array |
@a = (6,7,8); unshift @a, 5; shift @a;
|
<?php $a = array(6,7,8); array_unshift($a, 5); array_shift($a); ?>
|
a = [6,7,8] a.insert(0,5) a.pop(0)
|
a = [6,7,8] a.unshift(5) a.shift
|
array concatenation |
@a = (1,2,3); @b = (@a,(4,5,6)); push @a, (4,5,6);
|
<?php $a = array(1,2,3); $b = array_merge($a,array(4,5,6)); $a = array_merge($a,array(4,5,6)); ?>
|
a = [1,2,3] b = a + [4,5,6] a.extend([4,5,6])
|
a = [1,2,3] b = a + [4,5,6] a.concat([4,5,6])
|
address copy, shallow copy, deep copy |
@a = (1,2,[3,4]); $b = \@a; @c = @a; none
|
<?php $a = (1,2,array(3,4)); $b =& $a; none $d = $a; ?>
|
a = [1,2,[3,4]] b = a c = list(a) import copy d = copy.deepcopy(a)
|
a = [1,2,[3,4]] b = a c = a.dup d = Marshal.load(Marshal.dump(a))
|
arrays as function arguments |
|
|
|
|
array iteration |
for $i (1 2 3) { print "$i\n" }
|
<?php foreach (array(1,2,3) as $i) { echo "$i\n"; } ?>
|
for i in [1,2,3]: print(i)
|
[1,2,3].each { |i| puts I }
|
indexed array iteration |
@a = ('a','b','c'); for ($i=0; $i<scalar(@a); $i++) { print "$a[$i] at index $i\n"; };
|
<?php $a = array('a','b','c'); foreach ($a as $i => $c) { echo "$c at index $i\n"; } ?>
|
for i, c in enumerate(['a','b','c']): print("%s at index %d" % (c, i))
|
a = ['a','b','c'] a.each_with_index do |c,i| puts "#{c} at index #{i}" end
|
sort |
@a = (3,1,4,2); sort @a; @a = sort @a; sort { $a <=> $b } @a;
|
<?php $a = array(3,1,4,2); none sort($a); ?>
|
a = [3,1,4,2] sorted(a) a.sort()
|
a = [3,1,4,2] a.sort a.sort! a.sort { |m,n| m <=> n}
|
reverse |
@a = (1,2,3); reverse @a; @a = reverse @a;
|
<?php $a = array(1,2,3); array_reverse($a); $a = array_reverse($a); ?>
|
a = [1,2,3] list(reversed([1,2,3])) a.reverse()
|
a = [1,2,3] a.reverse a.reverse!
|
membership |
grep { 7 == $_ } @nums
|
<?php in_array(7, $nums) ?>
|
7 in nums
|
nums.include?(7)
|
intersection |
|
<?php $a = array(1,2); $b = array(2,3,4) array_intersect($a, $b) ?>
|
set.intersection(set([1,2]), set([2,3,4]))
|
[1,2] & [2,3,4]
|
union |
|
<?php ?>
|
set.union(set([1,2]),set([2,3,4]))
|
[1,2] | [2,3,4]
|
map |
map { $_ * $_ } (1,2,3)
|
<?php $t2 = create_function('$x', 'return $x*$x;') array_map($t2, array(1,2,3)) ?>
|
map(lambda x: x * x, [1,2,3]) # or use list comprehension: [x*x for x in [1,2,3]]
|
[1,2,3].map { |o| o*o }
|
filter |
grep { $_ > 1 } (1,2,3)
|
<?php $gt1 = create_function('$x','return $x>1;'); array_filter( array(1,2,3), $gt1) ?>
|
filter(lambda x: x > 1,[1,2,3]) # or use list comprehension: [x for x in [1,2,3] if x > 1]
|
[1,2,3].select { |o| o > 1 }
|
reduce |
reduce { $a + $b } 0, (1,2,3)
|
<?php $add = create_function('$a,$b','return $a+$b;'); array_reduce(array(1,2,3),$add,0) ?>
|
# import needed in python 3 only import reduce from functools reduce(lambda x,y:x+y,[1,2,3],0)
|
[1,2,3].inject(0) { |m,o| m+o }
|
universal test |
|
|
all(i%2 == 0 for i in [1,2,3,4])
|
[1,2,3,4].all? {|i| i.even? }
|
existential test |
|
|
any(i%2 == 0 for i in [1,2,3,4])
|
[1,2,3,4].any? {|i| i.even? }
|
map literal |
%h = ( 't' => 1, 'f' => 0 );
|
<?php $h = array('t' => 1, 'f' => 0); ?>
|
h = { 't':1, 'f':0 }
|
h = { 't' => 1, 'f' => 0 }
|
map size |
scalar(keys %h)
|
<?php count($h) ?>
|
len(h)
|
h.size
h.length # same as size
|
map lookup |
$h{'t'}
|
<?php $h['t'] ?>
|
h['t']
|
h['t']
|
is map key present |
defined($h{'y'})
|
<?php array_key_exists('y',$h); ?>
|
'y' in h
|
h.has_key?('y')
|
map iteration |
while ( ($k, $v) = each %h ) {
|
<?php foreach ($h as $k => $v ) { ?>
|
# python 2: for k,v in h.iteritems(): code # python 3: for k,v in h.items(): code
|
h.each { |k,v| code }
|
keys and values of map as arrays |
keys %h values %h
|
<?php array_keys($h) array_values($h) ?>
|
h.keys() h.values()
|
h.keys h.values
|
out of bounds behavior |
undef
|
<?php NULL ?>
|
|
nil
|