strings

スクリプト言語の比較

http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。

strings


































































































































  perl php python ruby
character literal

none


none


none


none
chr and ord

chr(65)
ord("A")

<?php
chr(65)
ord("A")
?>

chr(65)
ord('A')

65.chr
"A".ord

string literal

'don\'t say "no"'
"don't say \"no\""

<?php
'don\'t say "no"'
"don't say \"no\""
?>

'don\'t say "no"'
"don't say \"no\""

'don\'t say "no"'
"don't say \"no\""

newline in literal

yes


yes


no, use escape or
triple quote literal


yes
here document

$computer = 'PC';
$s = <<EOF;
here document
there $computer
EOF

<?php
$computer = 'PC';
$s = <<<EOF
here document
there $computer
EOF;
?>



none

computer = 'PC'
s = <<EOF
here document
there #{computer}
EOF

escapes

single quoted:
\' \\
double quoted:
\a \b \cx \e \f \n \r \t
\xhh \x{hhhh} \ooo

<?php
single quoted:
\' \\
double quoted:
\f \n \r \t \v
\xhh \$ \" \ooo
?>

\newline \\ \' \" \a \b
\f \n \r \t \v \ooo \xhh
# python 3:
\uhhhh

# single quoted:
\' \\
# double quoted:
\a \b \cx \e \f \n \r \s \t
\uhhhh \u{hhhhh} \v \xhh \ooo

encoding

<?php
?>

variable interpolation

my $count = 3;
my $item = "ball";
print "$count ${item}s\n";

<?php
$count = 3;
$item = "ball";
echo "$count ${item}s\n";
?>



none

count = 3
item = "ball"
puts "#{count} #{item}s"

length

length("hello")

<?php
strlen("hello")
?>

len('hello')

"hello".length
"hello".size

character count

"(3*(7+12))" =~ tr/(//

<?php
$a = count_chars("(3*(7+12))");
$a[ord('(')]
?>

'(3*(7+12))'.count('(')

'(3*(7+12))'.count('(')

index of substring

index("foo bar","bar")

<?php
strpos("foo bar", "bar")
?>

'foo bar'.index('bar')

"foo bar".index("bar")

extract substring

substr("foo bar",4,3)

<?php
substr("foo bar", 4, 3)
?>

"foo bar"[4:7]

"foo bar"[4,3]

concatenate

"hello, " . "world"

<?php
"hello, " . "world"
?>

'hello, ' + 'world'

"hello, " + "world"

replicate

my $hbar = '-' x 80;

<?php
$hbar = str_repeat('-', 80);
?>

hbar = '-' * 80

hbar = '-' * 80

split

split(/\s+/,"foo bar baz")

<?php
explode(" ","foo bar baz")
preg_split('/\s+/',"foo bar baz")
?>

'foo bar baz'.split()

"foo bar baz".split

join

join(' ',("foo","bar","baz"))

<?php
$a = array("foo","bar","baz");
implode(" ", $a)
?>

' '.join(['foo','bar','baz'])

['foo','bar','baz'].join(' ')

scan

none

<?php
$s = "foo bar baz";
preg_match_all('/\w+/', $s, $a);
$a[0]
?>

import re
s = 'foo bar baz'
re.compile('\w+').findall(s)

"foo bar baz".scan(/\w+/)

pack and unpack

my ($f, $s);
$f="a5ilfd";
$s=pack($f,"hello",7,7,3.14,3.14);
unpack($f,$s)

<?php
$f="a3ilfd";
$s=pack($f,"hello",7,7,3.14,3.14);
unpack("a3a/ii/ll/ff/dd",$s);
?>

import struct
f='5silfd'
s=struct.pack(f,'hello',7,7,3.14,3.14)
struct.unpack(f,s)

f="a5ilfd"
s=["hello",7,7,3.14,3.14].pack(f)
s.unpack(f)

sprintf

my $fmt = "foo: %s %d %f";
sprintf($fmt, 'bar', 13, 3.7)

<?php
$fmt = "foo: %s %d %f";
sprintf($fmt, 'bar', 13, 3.7);
?>

'foo: %s %d %f' % ('bar',13,3.7)
# new in python 2.6:
fmt = 'foo: {0} {1} {2}'
str.format(fmt, 'bar', 13, 3.7)

"foo: %s %d %f" % ['bar',13,3.7]

case manipulation

uc("hello")
lc("HELLO")
ucfirst("hello")

<?php
strtoupper("hello")
strtolower("HELLO")
ucfirst("hello")
?>

'hello'.upper()
'HELLO'.lower()
'hello'.capitalize()

"hello".upcase
"HELLO".downcase
"hello".capitalize

strip

use regex substitution

<?php
trim(" foo ")
ltrim(" foo")
rtrim("foo ")
?>

' foo '.strip()
' foo'.lstrip()
'foo '.rstrip()

" foo ".strip
" foo".lstrip
"foo ".rstrip

pad on right, on left

sprintf("%-10s","hello")
sprintf("%10s","hello")

<?php
str_pad("hello", 10)
str_pad("hello", 10, " ",
        STR_PAD_LEFT)
?>

'hello'.ljust(10)
'hello'.rjust(10)

"hello".ljust(10)
"hello".rjust(10)

character translation

$s = "hello";
$s =~ tr/a-z/n-za-m/;

<?php
$ins = implode(range('a','z'));
$outs = substr($ins,13,13) . substr($ins,0,13);
strtr("hello",$ins,$outs)
?>

from string import lowercase as ins
from string import maketrans
outs = ins[13:] + ins[:13]
"hello".translate(maketrans(ins,outs))

"hello".tr('a-z','n-za-m')

regexp match

"1999" =~ /^\d{4}$/
"foo BAR" =~ /^[a-z]+/
"foo BAR" =~ /[A-Z]+/

<?php
preg_match('/^\d{4}$/',"1999")
preg_match('/^[a-z]+/',"foo BAR")
preg_match('/[A-Z]+/',"foo BAR")
?>

import re
re.match("\d{4}$","1999")
re.match("[a-z]+","foo BAR")
re.search("[A-Z]+","foo BAR")

"1999".match(/^\d{4}$/)
"foo BAR".match(/^[a-z]+/)
"foo BAR".match(/[A-Z]+/)

match, prematch, postmatch

my $s = "A 17 B 12";
while ( $s =~ /\d+/ ) {
  my $discard = $`;
  my $number = $&;
  $s = $';
  print $number . "\n";
}



none

s = "A 17 B 12"
while True:
  m = re.search('\d+',s)
  if not m:
    break
  discard = s[0:m.start(0)]
  number = m.group()
  s = s[m.end(0):len(s)]
  print(s)

s = "A 17 B 12"
while (/\d+/.match(s)) do
  discard = $`
  number = $&
  s = $'
  puts number
end

substring matches

"2010-06-03" =~ /(\d{4})-(\d{2})-(\d{2})/;
($yr, $mn, $dy) = ($1, $2, $3);

<?php
$a = array();
$s = "2010-06-03";
$r = '/(\d{4})-(\d{2})-(\d{2})/';
preg_match($r, $s, $a);
list($_, $yr, $mn, $dy) = $a;
?>

import re
reg = "(\d{4})-(\d{2})-(\d{2})"
m = re.search(reg, "2010-06-03")
yr,mn,dy = m.groups()

reg = /(\d{4})-(\d{2})-(\d{2})/
m = reg.match("2010-06-03")
yr,mn,dy = m[1..3]

single substitution

$s = "foo bar bar";
$s =~ s/bar/baz/;
$s

<?php
$s = 'foo bar bar';
preg_replace('/bar/','baz',$s,1);
?>

import re
s = 'foo bar bar'
re.compile('bar').sub('baz', s, 1)

"foo bar bar".sub(/bar/,'baz')

global substitution

$s = "foo bar bar";
$s =~ s/bar/baz/g;
$s

<?php
$s = 'foo bar bar';
preg_replace('/bar/', 'baz', $s);
?>

import re
s = 'foo bar bar'
re.compile('bar').sub('baz', s)

"foo bar bar".gsub(/bar/,'baz')