top

TOP  RSS  Login

Perl と Python

Perl >

比較

Hello, World!

Perl

print "Hello, World!\n";
print STDOUT "Hello, STDOUT!\n";
print STDERR "Hello, STDERR!\n";

Python

print "Hello, World!"
import sys
sys.stdout.write("Hello, STDOUT!\n")
sys.stderr.write("Hello, STDERR!\n")

変数

Perl

my $var = 10;
my @arr = ( "mori", "koizumi", "abe", "fukuda" );
my %hash = (
    mori    => 387,
    koizumi => 1980,
    abe     => 366,
    fukuda  => 365
);

sub avg {
    my (%h) = @_;
    my $i = 0;
    $i += $_ for values(%h);
    return $i / scalar(keys(%h));
#    return $i / keys(%h);
}

print avg(%hash) . "\n";

Python

var = 10
arr = [ "mori", "koizumi", "abe", "fukuda" ]
hash = {
    "mori"    : 387,
    "koizumi" : 1980,
    "abe"     : 366,
    "fukuda"  : 365  
}

def avg(h):
    i = 0.0
    for n in h.values():
        i += n
    return i / len(h)

print avg(hash)

ファイルの読み書き

Perl

my $dat = "";

open(my $f, "+>", "/tmp/hogehoge") or die "error";
print $f "hello";
seek($f,0,0);
local($/) = undef;
$dat = <$f>;
close($f);

print $dat."\n";

Python

import sys

dat = ""

try:
    f = file("/tmp/hogehoge", "w+")
except:
    sys.stderr.write("error\n")
    sys.exit(1)

f.write("hello")
f.seek(0)
dat = f.read()
f.close()

print dat

正規表現によるマッチング

Perl

my $d = "AKEBONO";
if($d =~ m/^akebono$/i){
  print "akebono\n";
}

my $s = "Hello Barack, this is John speaking.";
if($s =~ m/^Hello (\w+), this is (\w+) speaking\.$/){
    print "match: $&\n";
    print "telephone: $1 and $2\n";
}

my @g = ($s =~ m/(\w+)/g);
if(@g){
    print join(", ", @g) ."\n";
}
#print join(", ", ($s =~ m/(\w+)/g)) ."\n";

Python

re.match() は先頭にマッチ、re.search() は任意の位置にマッチする。re.findall() はマッチした全ての文字列をリストとして返す。

import re

d = "AKEBONO"
m = re.match("akebono", d, re.I)
if m:
  print "akebono"

s = "Hello Barack, this is John speaking.";
m = re.search("^Hello (\w+), this is (\w+) speaking\.", s)
if m:
    print "match: " + m.group(0)
    print "telephone: " + m.group(1) + " and " + m.group(2)

g = re.findall("\w+", s)
if g:
    print ", ".join(g)

乱数

Perl

print int(rand(256)); # 0 - 255

my @arr = ( "AK", "AX", "EX", "RX", "CX", "TX" );
print $arr[rand($#arr + 1)];

Python

from random import *
print randint(0,255)

arr = [ "AK", "AX", "EX", "RX", "CX", "TX" ]
print choice(arr)

ソケット

Perl

use Socket;

my ($host,$port) = ("localhost",80);
my $addr = pack_sockaddr_in($port, inet_aton($host));

socket(my $sock, PF_INET, SOCK_STREAM, 0) or die "socket() failed";
connect($sock, $addr) or die "connect() failed";
select((select($sock), $|=1)[0]);

print $sock join("\r\n",
    "GET / HTTP/1.0",
    "Host: $host",
    "Connection: close",
    "",""
);

local($/) = undef;
print <$sock>;

close($sock);

Python

import socket
import sys

(host,port) = ("localhost",80)

data = ""

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    sock.connect((host,port))

    sock.sendall("\r\n".join([
        "GET / HTTP/1.0",
        "Host: " + host,
        "Connection: close",
        "",""
    ]))

    while True:
        d = sock.recv(8192)
        if not d: break
        data += d

    if sock: sock.close()

except:
    if sock: sock.close()
    sys.stderr.write("error")
    sys.exit(1)

sys.stdout.write(data)

Last modified:2008/10/04 12:57:42

0.130133