Skip to content

Instantly share code, notes, and snippets.

Avatar

OGATA Tetsuji xtetsuji

View GitHub Profile
@xtetsuji
xtetsuji / replace_sample.js
Last active Nov 26, 2020
Sample: convert from plural String#replace'es change to compact one reduce call. This script can be executed by GAS (some syntaxes require V8 engine support).
View replace_sample.js
function ordinally_replace_sample() {
const content = getContent();
let replaced = content;
replaced = replaced.replace(/&/g, "&");
replaced = replaced.replace(/</g, "&lt;");
replaced = replaced.replace(/>/g, "&gt;");
replaced = replaced.replace(/"/g, "&quot;");
replaced = replaced.replace(/'/g, "&apos;");
console.log("ordinally: " + replaced);
}
@xtetsuji
xtetsuji / urlopen
Created Sep 21, 2020
URL filter and open from STDIN
View urlopen
#!/usr/bin/env perl
# urlopen - open (macOS) URL from STDIN
# SYNOPSIS:
# tail url_include_log | urlopen
# stdout-url-command | urlopen
# OPTIONS:
# urlopen [--pass-through|-p] : pass through STDOUT (default off)
# urlopen [--verbose|-v] : outout opend URL (default off)
use v5.12;
use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help);
@xtetsuji
xtetsuji / sqrt-newton.pl
Created Sep 17, 2020
Get square root by Newton's method.
View sqrt-newton.pl
#!/usr/bin/env perl
use v5.14;
use constant MAX_NEWTON_ITERATION_COUNT => 8;
print "平方根をニュートン法で求めます\n";
print "1より大きい自然数を入れて下さい: ";
my $input = <STDIN>;
chomp $input;
if ( !$input || $input <= 1 ) {
die "1より大きい自然数ではありませんでした\n";
@xtetsuji
xtetsuji / parse-headers-regexp.pl
Last active Nov 11, 2019
parse mail headers by zero-length assertion regular expression (negative lookahead)
View parse-headers-regexp.pl
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $header_string = <<'END_HEADER';
Delivered-To: xtetsuji@example.jp
Received: by 2002:ff68:12bc:0:0:0:0:0 with SMTP id 2f8a1000492ce44c1b7c;
Fri, 8 Nov 2019 15:07:34 -0800 (PST)
X-Received: by 2002:a63:6581:: with SMTP id c451320e8476820844296b80587f9f3a;
@xtetsuji
xtetsuji / one-liner-output.txt
Created Oct 6, 2019
what do meta character \s matches?
View one-liner-output.txt
$ perl -e 'for my $digit (0x00..0xff) { my $byte = chr $digit; my $match = $byte =~ /^\s$/; printf "0x%x\t%s\n", $digit, $match ? "match" : "-"; }'
0x00 -
0x01 -
0x02 -
0x03 -
0x04 -
0x05 -
0x06 -
0x07 -
0x08 -
@xtetsuji
xtetsuji / eccentric-sequence-re.pl
Last active Jun 21, 2019
print 1..100 without loop, recursive, goto by Perl (not C++)
View eccentric-sequence-re.pl
#!/usr/bin/env perl
use strict;
use warnings;
my $str = "A " x 100;
my $i = 1;
$str =~ s/A/ $i++ /eg;
print "$str\n";
@xtetsuji
xtetsuji / kadai-20190512.md
Created May 13, 2019
2019/05/12 ピザ会の課題
View kadai-20190512.md

2019/05/12 ピザ会の課題

正弦波 y = sin(x) を書く

参考例

  • 0.05秒に1回プロットする
  • x は 0 から pi/10 ずつ増えていく
  • sin(x) は -1 から 1 までの値を取るので、10倍して +10 すると 0 から 20 までの値となる。これの整数部分の個数 "*" を描く
    • int( 10 * sin ( $x ) + 10 )
@xtetsuji
xtetsuji / kanji-numerals-re.pl
Last active Jun 16, 2019
match regular expression of 1..99 kanji numerals
View kanji-numerals-re.pl
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
# 漢数字 1..9 の一文字 (need: use utf8)
my $kd_re = qr/[一二三四五六七八九]/;
# 1..99 の漢数字表現
my $kanji_numerals_re = qr{
@xtetsuji
xtetsuji / guess-ad-year.pl
Created Apr 13, 2019
Perl入学式 第1回 ピザ会 課題 2019/04/13
View guess-ad-year.pl
#!/usr/bin/perl
# guess-ad-year.pl - 和暦を引数に与えると西暦年を応えてくれる
# ad は A.D. (Anno Domini = 西暦年) の略
#
# 例:
# guess-ad-year.pl 平成30年
# 2018年
# guess-ad-year.pl 昭和54年
# 1979年
@xtetsuji
xtetsuji / suggest-two-kanjis.pl
Last active Mar 31, 2019
Perl kanji processing.
View suggest-two-kanjis.pl
#!/usr/bin/perl
# see: http://www.asahi-net.or.jp/~ax2s-kmtn/ref/unicode/cjku_klist.html
use strict;
use warnings;
use utf8;
binmode STDOUT, ':utf8';
# ほぼ Unicode 全漢字を集める
my ($start, $end) = (0x4E00, 0x9FAF);
You can’t perform that action at this time.