2023年9月14日 (木)

ファイル内の文字列置換

use utf8;
use Encode;

binmode STDOUT, ':encoding(cp932)';

my $file = encode('cp932', '入力.txt');
my $out = encode('cp932', '出力.txt');

open my $fhi, "<:utf8", $file;
open my $fho, ">:utf8", $out;

my $content = do { local $/; <$fhi> };

my $a = quotemeta 'http://abc.com/';
my $b = 'http://xyz.com/';
$content =~ s/${a}/${b}/g;

print $fho $content;

close $fho;
close $fhi;

| | コメント (0)

2022年10月24日 (月)

Date の拡張

if (!Date.prototype.toArray) {
  Date.prototype.toArray = function() {
    return [
      this.getFullYear(),
      this.getMonth(),
      this.getDate(),
      this.getHours(),
      this.getMinutes(),
      this.getSeconds(),
      this.getMilliseconds()
    ];
  };
}

if (!Date.today) {
  Date.today = function(...args) {
    var r = new Date().toArray()
      .slice(0, 3).map(function(x, i) {
      return x + (args[i] | 0);
    });
    return new Date(...r).getTime();
  };
}

|

2022年2月21日 (月)

JQuery - 外部リンク自動判定

jQuery(function($) {
  var icon = 'http://aok.blue.coocan.jp/images/icon_blank.gif';
  $('a[href^="http://"],a[href^="https://"]').each(function() {
    var host = new URL(document.baseURI).host;
    if (this.host != host) {
      $(this).not(':has(img)').after(`<img src="${icon}" />`);
      $(this).attr('target', '_blank');
    }
  });
});

» 続きを読む

| | コメント (0)

2021年1月12日 (火)

都道府県ソート

var pref_sort = function(arr, n) {
  var pref47 = [
    '北海道', '青森県', '岩手県', '宮城県', '秋田県',
    '山形県', '福島県', '茨城県', '栃木県', '群馬県',
    '埼玉県', '千葉県', '東京都', '神奈川県', '新潟県',
    '富山県', '石川県', '福井県', '山梨県', '長野県',
    '岐阜県', '静岡県', '愛知県', '三重県', '滋賀県',
    '京都府', '大阪府', '兵庫県', '奈良県', '和歌山県',
    '鳥取県', '島根県', '岡山県', '広島県', '山口県',
    '徳島県', '香川県', '愛媛県', '高知県', '福岡県',
    '佐賀県', '長崎県', '熊本県', '大分県', '宮崎県',
    '鹿児島県', '沖縄県'
  ];
  return arr.sort(function(a, b) {
    /* 都道府県ソート */
    var ai = pref47.indexOf(a[n]);
    var bi = pref47.indexOf(b[n]);
    if (ai < bi) return -1;
    if (ai > bi) return 1;
    return 0;
  });
};

|

2020年2月19日 (水)

RGB888 => RGB565

var toRGB565 = function(RGB888) {
  var r = RGB888 >> 8 & 0xf800;
  var g = RGB888 >> 5 & 0x7e0;
  var b = RGB888 >> 3 & 0x1f;
  return r | g | b;
};

| | コメント (0)

«配列どうしのOR検索