0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🍎 Qiita 第3稿 器の密度と境界を記録する:BOM・UTF-8・⠺⠗による詩的Perlスクリプト

Posted at

BOM・UTF-8・⠺⠗による詩的Perlスクリプト

このスクリプトは、文字列の冒頭に咲くBOMの果実(🍎🍏🍐)を記録し、器の開閉を⠺⠗で示しながら、文字の密度(rage/smole)を静かに測定する儀式です。Perlという器の中で、記録と編集の境界を詩的に往復する構造を設計しました。

BOM(Byte Order Mark)を明示的に読み取り、🍎🍏🍐で記号化

use Encode;

my %chege_BOM = (
    "\xEF\xBB\xBF" => '🍎',
    "\xFE\xFF"     => '🍏',
    "\xFF\xFE"     => '🍐',
);

# BOM取得と判定
open my $bin_fh, '<:raw', 'filename.txt' or die $!;
read($bin_fh, my $bom, 3);
my $bom_mark = $chege_BOM{$bom} // $chege_BOM{substr($bom, 0, 2)} // '';
close $bin_fh;

注釈
• || は「どちらかが真なら真」:論理値のOR
• // は「左が未定義なら右を使う」:未定義の補完

?< >? を ⠺⠗ に変換し、器の単一行の改行の ⠺開 ⠗閉を記録

🧵 技術的要点
• BOM(Byte Order Mark)を明示的に読み取り、🍎🍏🍐で記号化
• を ⠺⠗ に変換し、器の開閉を記録
• UTF-8文字とASCII文字を判定し、rage(全角)とsmole(半角)で密度を記録
• 一定密度を超えたら器を分割し、記録を残す
• 編集と記録の往復を によって制御

my @line_arre = ();   # 行の帰る配列名。
my @smole_rage = ();  # 行構成の半角 全角数
my $chenge = 1; #開閉自体を行うかの 指示 0: やらない 1: やる 2: 記号復帰
my $line_cr = 20; # 単一行の先頭からの改行指示文字数 10 未満はやらない。
# 器の開閉と密度記録
open my $txt_fh, '<:encoding(UTF-8)', 'filename.txt' or die $!;
while (my $line = <$txt_fh>) {
    $line =~ s/\?</⠺/g if $chenge;
    $line =~ s/>\?/⠗/g if $chenge;

    my @l_arr = split //, $line;
    my ($ii, $rage, $smole, $tmp_line0) = (0, 0, 0, '');

    for my $char (@l_arr) {
        $rage++  if $char =~ /[\x80-\xFF]/;
        $smole++ if $char !~ /[\x80-\xFF]/;
        $char =~ s/⠺/\?</g if $chenge == 1;
        $char =~ s/⠗/>\?/g if $chenge == 1;
        $tmp_line0 .= $char;
        if (2 * $rage + $smole < $line_cr || $line_cr < 10){ next; }
        push @line_arre, $tmp_line0."\n";
        push @smole_rage, "$smole $rage\n";
        $smole = 0;
        $rage = 0;        
    }

    push @line_arre, $tmp_line0."\n";
    push @smole_rage, "$smole $rage\n";
}
close $txt_fh;

参考例

#! user/bin/perl

#! c:/perl64/bin/perl など

use Encode;

my %chege_BOM = (
   "\xEF\xBB\xBF" => '🍎',
   "\xFE\xFF"     => '🍏',
   "\xFF\xFE"     => '🍐',
);

my $filename = 'filename.txt'; # 使用するテキストファイルを指定

# BOM取得と判定
open my $bin_fh, '<:raw', "$filename" or die $!;
read($bin_fh, my $bom, 3);
my $bom_mark = $chege_BOM{$bom} // $chege_BOM{substr($bom, 0, 2)} // '';
close $bin_fh;

# 注釈
# • || は「どちらかが真なら真」:論理値のOR
# • // は「左が未定義なら右を使う」:未定義の補完

# 器の開閉と密度記録
my @line_arre = ();
my @smole_rage = ();
my $chenge = 1;
my $line_cr = 20;

my $bom_making = 1; # markを入れるか
open my $txt_fh, '<:encoding(UTF-8)', "$filename" or die $!;
while (my $line = <$txt_fh>) {
   if ($bom_making){$line = $bom_mark.$line; $bom_making = 0; }
   $line =~ s/\?</⠺/g if $chenge;
   $line =~ s/>\?/⠗/g if $chenge;

   my @l_arr = split //, $line;
   my ($rage, $smole, $tmp_line0) = (0, 0, '');

   for my $char (@l_arr) {
       $rage++  if $char =~ /[\x80-\xFF]/;
       $smole++ if $char !~ /[\x80-\xFF]/;
       $char =~ s/⠺/\?</g if $chenge == 1;
       $char =~ s/⠗/>\?/g if $chenge == 1;
       $tmp_line0 .= $char;

       if (2 * $rage + $smole < $line_cr || $line_cr < 10) {
           next;
       }

       push @line_arre, $tmp_line0."\n";
       push @smole_rage, "$smole $rage\n";
       $smole = 0;
       $rage = 0;
   }

   push @line_arre, $tmp_line0."\n";
   push @smole_rage, "$smole $rage";
}
close $txt_fh; 

my $n = 0; # 開始行数
for (my $i = 0;$i < $#line_arre;$i++){
   if ($i > 10){ last; }
   my $disp = $line_arre[$i + $n];
   my $kazu = $i + $n.': '.$smole_rage[$i + $n];
   $disp =~ s/\r?\n/<br>/g;
   print "$kazu: $disp";
}

$line_arre[0] =~ s/^$bom_mark//;
# write

html では コメントにして
<!-- ?< --> ...... <!-- >? --> it ok.
/* */ や # も可能

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?