Perlのパターンマッチサイズの上限は

79 views
Skip to first unread message

jscripter

unread,
Sep 24, 2013, 9:42:40 AM9/24/13
to tsnet_...@googlegroups.com
毎度です。例によってパターンマッチを振り回してプログラムを書いているのですが、400KBぐらいのテキスト全体をマッチさせようとしているのです。無理ですかね・・・

jscripter

unread,
Sep 25, 2013, 7:51:37 AM9/25/13
to tsnet_...@googlegroups.com
5.008007についての一つの答え

パターンマッチングの限界?(最大文字数) | PerlのQ&A【OKWave】 http://okwave.jp/qa/q1493606.html

Complex regular subexpression recursion limit (32766) exceeded

2013年9月24日火曜日 22時42分40秒 UTC+9 jscripter:
毎度です。例によってパターンマッチを振り回してプログラムを書いているのですが、400KBぐらいのテキスト全体をマッチさせようとしているのです。無理ですかね・・・

jscripter

unread,
Sep 25, 2013, 9:19:26 AM9/25/13
to tsnet_...@googlegroups.com
下記の64bit Windows用Perlを使っているが、テキストのサイズの問題ではないようだ^^;)484KBでも通るテキストがある。おかしいなあ・・・

This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

Binary build 1601 [296175] provided by ActiveState http://www.ActiveState.com
Built Aug 30 2012 18:41:50

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


2013年9月25日水曜日 20時51分37秒 UTC+9 jscripter:

davi

unread,
Sep 25, 2013, 10:18:12 AM9/25/13
to tsnet_...@googlegroups.com
jscripterさん < こん??は でび@プチ逃避 です

On Wed, 25 Sep 2013 06:19:26 -0700 (PDT)
jscripter <jscri...@gmail.com> wrote:

> 下記の64bit
> Windows用Perlを使っているが、テキストのサイズの問題ではないようだ^^;)
>484KBでも通るテキストがある。おかしいなあ・・・

Perlは全然わかりませんが、ちょっと検索してみました。

http://www.perlmonks.org/?node_id=810857
http://d.hatena.ne.jp/gfx/20110212/1297512479

この辺ですかね?

ネストが深いXMLデータのパースをやろうとすると、
400KBを軽く超えてしまう場合もあるかもしれませんね。

そう考えると、Webブラウザって、すごいんですね。きっと。

でび http://davi.txt-nifty.com/1984/

jscripter

unread,
Sep 25, 2013, 10:27:28 AM9/25/13
to TSNET
コメントありがとうございます。

enchantMOONというタブレットのJSON形式の線画データを読んで、JavaScriptで描画するCGIを書いているのです。JSONを読み書きするライブラリもあるのですが、取り敢えずパターンマッチを使ってしまうのですね^^;)定型的なデータなので、なぜ読めたり読めなかったりするのか、調べているのです。

こんな感じ。

($strokesdata{$sticker}) = ($stickerinfo =~ /\[(({"width":[\+\-\d\.]+,"color":[\+\-\d\.]+,"type":"pen","data":\[[\d\.,]+\]},?)+)\]/);



2013年9月25日 23:18 davi <davi...@nifty.com>:

--
このメールは Google グループのグループ「TSNET」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、tsnet_member...@googlegroups.com にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

jscripter

unread,
Sep 25, 2013, 11:00:06 AM9/25/13
to tsnet_...@googlegroups.com
JSONデータはこんな感じ。

{"version":"0.2","x":0,"y":0,"width":768,"height":1024,"scale":1.0,"color":-16777216,"transparent":false,"strokes":[{"width":2.5,"color":-1,"type":"pen","data":[465.84375,61.07143,0.084999785,465.375,60.285717,0.42846295,465.0,59.714287,0.49568665,464.71875,59.285717,0.48482963]},{"width":2.5,"color":-1,"type":"pen","data":[452.71875,62.500004,0.12085689,452.53125, ... ]},{ ...}
,{"width":2.5,"color":-1,"type":"pen","data":[698.0625,582.0715,0.40160966,699.0,582.1429,0.48637345,699.65625,582.1429,0.52223295,700.5,582.3572,0.55753964,701.0625,582.5715,0.6086699,701.53125,582.8572,0.6213849,701.90625,583.3572,0.62997776,702.28125,583.78577,0.6353854,702.65625,584.4286,0.636922,702.9375,585.0715,0.6353854,703.3125,585.6429,0.63461566,703.59375,586.3572,0.6315275,703.875,587.1429,0.6205979,704.25,587.8572,0.5973221,704.625,588.6429,0.554022]}]}

2013年9月25日水曜日 23時27分28秒 UTC+9 jscripter:
コメントありがとうございます。

enchantMOONというタブレットのJSON形式の線画データを読んで、JavaScriptで描画するCGIを書いているのです。JSONを読み書きするライブラリもあるのですが、取り敢えずパターンマッチを使ってしまうのですね^^;)定型的なデータなので、なぜ読めたり読めなかったりするのか、調べているのです。

こんな感じ。

($strokesdata{$sticker}) = ($stickerinfo =~ /\[(({"width":[\+\-\d\.]+,"color":[\+\-\d\.]+,"type":"pen","data":\[[\d\.,]+\]},?)+)\]/);

2013年9月25日 23:18 davi <davi...@nifty.com>:
jscripterさん  <  こん??は でび@プチ逃避 です

On Wed, 25 Sep 2013 06:19:26 -0700 (PDT)
jscripter <jscri...@gmail.com> wrote:

> 下記の64bit
> Windows用Perlを使っているが、テキストのサイズの問題ではないようだ^^;)
>484KBでも通るテキストがある。おかしいなあ・・・

Perlは全然わかりませんが、ちょっと検索してみました。

http://www.perlmonks.org/?node_id=810857
http://d.hatena.ne.jp/gfx/20110212/1297512479

この辺ですかね?

ネストが深いXMLデータのパースをやろうとすると、
400KBを軽く超えてしまう場合もあるかもしれませんね。

そう考えると、Webブラウザって、すごいんですね。きっと。

でび  http://davi.txt-nifty.com/1984/

--
このメールは Google グループのグループ「TSNET」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、tsnet_members+unsubscribe@googlegroups.com にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

davi

unread,
Sep 25, 2013, 11:24:05 AM9/25/13
to tsnet_...@googlegroups.com
jscripterさん < こん??は でび です

On Wed, 25 Sep 2013 08:00:06 -0700 (PDT)
jscripter <jscri...@gmail.com> wrote:

> JSONデータはこんな感じ。

この数字の羅列は、SVG画像形式のようなものですかね?

http://leoclock.blogspot.jp/2009/01/blog-post_27.html
の真ん中辺りに (実用上の補足) という部分がありました。

そこにPerlでのJSONハックのサンプルコードと「Perl6のRulesという機能」
がどうの…っていう紹介が書かれていました。

私には何を言っているのか全くわかりませんでしたが。

でび http://davi.txt-nifty.com/1984/

jscripter

unread,
Sep 26, 2013, 7:26:43 AM9/26/13
to TSNET
でひさん、いろいろとありがとうございます。正規表現によるパターンマッチもif~elsif~else構文を使って複雑になってくるとなぜマッチするのか次第にわからなくなります^^;)

今回の例は、"strokes":[{"width":...."data":[1234,123,0.123,1234,123,0.123,....]},{"width":...."data":[1234,123,0.123,1234,123,0.123,....]},...,{...}]の
"strokes":[...]の[...]に囲まれている部分を取り出すだけなので比較的単純です。

"data":[...]の中の数値は、x座標、y座標、透明度のパラメータが繰り返して現れています。



2013年9月26日 0:24 davi <davi...@nifty.com>:
--
このメールは Google グループのグループ「TSNET」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、tsnet_member...@googlegroups.com にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

jscripter

unread,
Sep 27, 2013, 10:05:46 AM9/27/13
to tsnet_...@googlegroups.com
様様なサンプルについてさらに試したのですが、原因が明らかな場合とよくわからない場合があるので、パターンマッチは一旦止めて、JSONの読み書きのライブラリを試してみようと思います。ここに書くことによって、いただいたコメントについて調べたりしているうちに、頭の中を整理したりできるので、問題を解決する方向が次第に決まってきます。ありがとうございます。

2013年9月26日木曜日 20時26分43秒 UTC+9 jscripter:
でひさん、いろいろとありがとうございます。正規表現によるパターンマッチもif〜elsif〜else構文を使って複雑になってくるとなぜマッチするのか次第にわからなくなります^^;)

今回の例は、"strokes":[{"width":...."data":[1234,123,0.123,1234,123,0.123,....]},{"width":...."data":[1234,123,0.123,1234,123,0.123,....]},...,{...}]の
"strokes":[...]の[...]に囲まれている部分を取り出すだけなので比較的単純です。

"data":[...]の中の数値は、x座標、y座標、透明度のパラメータが繰り返して現れています。

2013年9月26日 0:24 davi <davi...@nifty.com>:
jscripterさん  <  こん??は でび です

On Wed, 25 Sep 2013 08:00:06 -0700 (PDT)
jscripter <jscri...@gmail.com> wrote:

> JSONデータはこんな感じ。

この数字の羅列は、SVG画像形式のようなものですかね?

http://leoclock.blogspot.jp/2009/01/blog-post_27.html
の真ん中辺りに (実用上の補足) という部分がありました。

そこにPerlでのJSONハックのサンプルコードと「Perl6のRulesという機能」
がどうの…っていう紹介が書かれていました。

私には何を言っているのか全くわかりませんでしたが。

でび  http://davi.txt-nifty.com/1984/

--
このメールは Google グループのグループ「TSNET」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、tsnet_members+unsubscribe@googlegroups.com にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

jscripter

unread,
Sep 27, 2013, 6:21:05 PM9/27/13
to TSNET
入力データに直接あたって調べて、strokesの内容が出力されない原因が明らかな場合とは、データが空の場合です。よくわからない場合とは、print文のデバッグでは入力データの取得は成功しているのですが、マッチングによる取り出しが空になっている。すなわちマッチングが成功していない。


2013年9月27日 23:05 jscripter <jscri...@gmail.com>:
このグループから退会し、メールの受信を停止するには、tsnet_member...@googlegroups.com にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。

jscripter

unread,
Sep 28, 2013, 5:54:59 AM9/28/13
to tsnet_...@googlegroups.com

JSONのライブラリモジュールを使ってみました。うまく動きましたね。複雑なデータ構造へのアクセスに慣れていないので、ちょっと途惑いましたが、ハッシュの配列とかハッシュのハッシュとか、うまくアクセスできるものです。大変勉強になりました。JavaScriptによる画像表現も少しわかってきました。どう書けば、複数の画像や線画を表示できるのかが最初はよくわからなくて、見様見真似で・・・なんとか動いてよかった^^;)JavaScriptもちょっとしたことで動いたり動かなかったりするし^^;;;PerlとJavaScript両方デバッグする必要がある。

後は、もう少しenchantMOONの仕様と機能を調べる必要があります。下記のスクリプトは画像や線画をCANVASに配置する程度のものです。ページ間のリンクはまだ完ぺきではない。本が出るはずですけどね。もっともユーザーは4000人ぐらいしか存在しないはずですが。買う人がそのうちどれくらいいるかですね。

-----^
#!/Perl64/bin/perl.exe
#
use CGI qw(:cgi);
use JSON; # imports encode_json, decode_json, to_json and from_json.
 
my $server = "http://xxx.xxx.xxx.xxx/cgi-bin/enchantMOON";# localserverのCGIディレクトリ
my $emoondir = "X:/xxxxxx/yyyyy/enchantMOON/Data/MyNotebook1";# enchantMOONページデータディレクトリ


(my $thisscript = $0) =~ s/^.+[\\\/]([^\\\/]+)$/$1/;
my $page = param('page');
unless($page){
$page = "0PQQ21081376620923240";
}
my $pageinfo = "";
my @stickers = ();
my $sticker = "";
my $stickerinfo = "";
my $hackinfo = "";
my %stickerdata = ();

open my $fh, $emoondir . "/$page/info.json";
$pageinfo = <$fh>;
close $fh;

@stickers = ($pageinfo =~ /"(.{21})"/g);

my $json;
my %perl_scalar = ();

foreach $sticker (@stickers){
open my $fh, $emoondir . "/$page/$sticker/info.json" or die "Can't open it!?: $!";
$stickerinfo = <$fh>;
close $fh;

$json = JSON->new->allow_nonref;
$perl_scalar{$sticker} = $json->decode( $stickerinfo );
if(open $fh, $emoondir . "/$page/$sticker/hack.js"){
$hackinfo = <$fh>;
if($hackinfo =~ /location.replace\("([^"]+)"\);/){
$stickerdata{$sticker} = $1;
$stickerdata{$sticker} =~ s/^page:\/\/(.+)$/$server\/$thisscript\?page=$1/;
}else{
$stickerdata{$sticker} = "$server\/$thisscript\?page=$page";
}
close $fh;
}else{
$stickerdata{$sticker} = "";
}
}

print <<"CANVASTOP";
Content-type: text/html

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<style>
body {
widht: 100%;
background: #000000;
}

.footer {
font-size: 80%;
color: white;
text-align: right;
margin-top: 10pt;
text-align: right;
}

.footer a {
color: white;
text-decolation: none;
}

#canvas {
    border: 0px;
    padding: 0p;
}

#frame {
    display: hidden;
    border: 0px;
    padding: 0p;
}

</style>
</head>
<body>
<div style="width: 768px; margin: auto;">
    <div id="frame">
 <canvas width="768" height="1024" id="canvas"></canvas>
    </div>
</div>
CANVASTOP

foreach $sticker (keys %stickerdata){

if($stickerdata{$sticker}){

print <<"IMAGE";
<script>
window.addEventListener("load", function(){
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var img = new Image();
img.src = "/enchantMOON/Data/MyNotebook1/${page}/${sticker}/background.png";
img.onload = function() {
context.drawImage(img, $perl_scalar{$sticker}->{x}, $perl_scalar{$sticker}->{y});
};

canvas.addEventListener("mousedown", mouseDownListner, false);
function mouseDownListner(e) {
var rect = e.target.getBoundingClientRect();
mouseX1 = e.clientX - rect.left;
mouseY1 = e.clientY - rect.top;
if (mouseX1 > $perl_scalar{$sticker}->{x} && mouseX1 < $perl_scalar{$sticker}->{x} + $perl_scalar{$sticker}->{width}) {
if (mouseY1 > $perl_scalar{$sticker}->{y} && mouseY1 < $perl_scalar{$sticker}->{y} + $perl_scalar{$sticker}->{height}) {
    location.href = "$stickerdata{$sticker}";
}
}
}
}, false);
</script>
IMAGE

}

}

my $strokecolorhex = "";
my $c = 0;
my $s = 0;
my $i = -1;
my $j = -1;

foreach $sticker (keys %perl_scalar){
while($perl_scalar{$sticker}->{strokes}->[++$i]){

print <<"STROKEBEGIN";
<script>
window.addEventListener("load", function(){
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.beginPath();
STROKEBEGIN

while($perl_scalar{$sticker}->{strokes}->[$i]->{data}->[++$j]){
++$c;++$s;
if($s == 1){
print "context.moveTo($perl_scalar{$sticker}->{strokes}->[$i]->{data}->[$j],";
}elsif($c == 1){
print "context.lineTo($perl_scalar{$sticker}->{strokes}->[$i]->{data}->[$j],";
}elsif($c == 2){
print " $perl_scalar{$sticker}->{strokes}->[$i]->{data}->[$j]);\n";
}elsif($c == 3){
print "globalAlpha = $perl_scalar{$sticker}->{strokes}->[$i]->{data}->[$j];\n";
$c = 0;
next;
}
}
$c = 0;$s = 0;$j = -1;
($strokecolorhex = sprintf "%X", $perl_scalar{$sticker}->{strokes}->[$i]->{color}) =~ s/^FFFFFFFFFF(\w{6})$/"#$1"/;

print <<"STROKE";
context.strokeStyle = $strokecolorhex;
context.lineWidth = $perl_scalar{$sticker}->{strokes}->[$i]->{width};
context.stroke();
}, false);
</script>
STROKE

}
$i = -1;

}

print <<"CANVASEND";
</body>
</html>
CANVASEND
-----$

2013年9月27日金曜日 23時05分46秒 UTC+9 jscripter:
Reply all
Reply to author
Forward
0 new messages