150 lines
9.7 KiB
Markdown
150 lines
9.7 KiB
Markdown
---
|
||
title: 'isbn13 –> isbn10'
|
||
author: kazu634
|
||
date: 2008-07-02
|
||
wordtwit_post_info:
|
||
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4107;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- Perl
|
||
- Programming
|
||
|
||
---
|
||
<div class="section">
|
||
<p>
|
||
本とかを買ってきて、
|
||
</p>
|
||
|
||
<ol>
|
||
<li>
|
||
isbn13で検索
|
||
</li>
|
||
<li>
|
||
本の題名で検索してAmazonにたどり着く
|
||
</li>
|
||
<li>
|
||
asinをコピペして、ブログ記事を書く
|
||
</li>
|
||
</ol>
|
||
|
||
<p>
|
||
というこの一連の流れがうざったくなってきたので、perlでisbn13をisbn10に変換するスクリプトを書いてみた。スクリプトの引数チェックが甘いけれど、まぁ引数は数字の羅列で先頭13桁目までしか見ないのは仕様ということで(..;)
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synStatement">use strict</span>;
|
||
<span class="synStatement">use warnings</span>;
|
||
<span class="synComment"># ==================</span>
|
||
<span class="synComment"># === 変数の宣言 ===</span>
|
||
<span class="synComment"># ==================</span>
|
||
<span class="synComment"># isbn10を格納するための配列</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">@isbn10</span>;
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$isbn13</span>;
|
||
<span class="synComment"># ====================</span>
|
||
<span class="synComment"># === 起動時の処理 ===</span>
|
||
<span class="synComment"># ====================</span>
|
||
<span class="synComment"># スクリプトに引数が設定されてなければ終了</span>
|
||
<span class="synStatement">exit</span> <span class="synStatement">if</span> (<span class="synIdentifier">@ARGV</span> == <span class="synConstant"></span>);
|
||
<span class="synComment"># スクリプトの引数を代入する</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$chk</span> = <span class="synIdentifier">&chk_arg</span>(<span class="synIdentifier">$ARGV</span>[<span class="synConstant"></span>]);
|
||
<span class="synComment"># 引数のチェック</span>
|
||
<span class="synStatement">exit</span> <span class="synStatement">if</span> (<span class="synIdentifier">$chk</span> == <span class="synConstant">-1</span>);
|
||
<span class="synIdentifier">$isbn13</span> = <span class="synIdentifier">$ARGV</span>[<span class="synConstant"></span>];
|
||
<span class="synComment"># ================</span>
|
||
<span class="synComment"># === 処理開始 ===</span>
|
||
<span class="synComment"># ================</span>
|
||
<span class="synComment"># 数字を一つずつ配列に格納</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">@temp</span> = <span class="synStatement">split</span>(<span class="synStatement">//</span>, <span class="synIdentifier">$isbn13</span>);
|
||
<span class="synComment"># check digit以外の部分が完成</span>
|
||
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant">3</span>; <span class="synIdentifier">$i</span> < <span class="synConstant">12</span>; <span class="synIdentifier">$i</span>++) {
|
||
<span class="synStatement">push</span>(<span class="synIdentifier">@isbn10</span>, <span class="synIdentifier">$temp</span>[<span class="synIdentifier">$i</span>])
|
||
}
|
||
<span class="synComment"># check digitの計算</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$c_digit</span> = <span class="synIdentifier">&calc_c_digit</span>(<span class="synIdentifier">@isbn10</span>);
|
||
<span class="synComment"># check digitを配列に格納</span>
|
||
<span class="synStatement">push</span>(<span class="synIdentifier">@isbn10</span>, <span class="synIdentifier">$c_digit</span>);
|
||
<span class="synComment"># isbn10を表示</span>
|
||
<span class="synStatement">print</span>(<span class="synIdentifier">@isbn10</span>, <span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
<span class="synComment"># ================</span>
|
||
<span class="synComment"># === 処理終了 ===</span>
|
||
<span class="synComment"># ================</span>
|
||
<span class="synStatement">exit</span>;
|
||
<span class="synComment"># ==================================</span>
|
||
<span class="synComment"># ========== サブルーチン ==========</span>
|
||
<span class="synComment"># ==================================</span>
|
||
<span class="synComment"># ================================</span>
|
||
<span class="synComment"># === チェックディジットの計算 ===</span>
|
||
<span class="synComment"># ================================</span>
|
||
<span class="synComment"># 戻り値: チェックディジット</span>
|
||
<span class="synStatement">sub</span><span class="synIdentifier"> calc_c_digit </span>{
|
||
<span class="synComment"># check digitを計算するためのisbn</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">@tmp_isbn9</span> = <span class="synIdentifier">@_</span>;
|
||
<span class="synComment"># 計算用</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$temp</span>;
|
||
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synIdentifier">$i</span> < <span class="synIdentifier">@tmp_isbn9</span>; <span class="synIdentifier">$i</span>++) {
|
||
<span class="synIdentifier">$temp</span> += <span class="synIdentifier">$tmp_isbn9</span>[<span class="synIdentifier">$i</span>] * (<span class="synConstant">10</span> - <span class="synIdentifier">$i</span>);
|
||
}
|
||
<span class="synIdentifier">$temp</span> = <span class="synStatement">int</span>(<span class="synIdentifier">$temp</span> % <span class="synConstant">11</span>);
|
||
<span class="synIdentifier">$temp</span> = <span class="synConstant">11</span> - <span class="synIdentifier">$temp</span>;
|
||
<span class="synStatement">if</span> (<span class="synIdentifier">$temp</span> == <span class="synConstant">10</span>) {
|
||
<span class="synStatement">return</span> (<span class="synConstant">"X"</span>);
|
||
} <span class="synStatement">else</span> {
|
||
<span class="synStatement">return</span> <span class="synIdentifier">$temp</span>;
|
||
}
|
||
}
|
||
<span class="synComment"># ================================</span>
|
||
<span class="synComment"># === スクリプトの引数チェック ===</span>
|
||
<span class="synComment"># ================================</span>
|
||
<span class="synComment"># 0 : 正常な引数</span>
|
||
<span class="synComment"># -1 : 異常な引数</span>
|
||
<span class="synStatement">sub</span><span class="synIdentifier"> chk_arg </span>{
|
||
<span class="synStatement">my</span> <span class="synIdentifier">@tmp</span> = <span class="synIdentifier">@_</span>;
|
||
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$i</span> (<span class="synIdentifier">@tmp</span>) {
|
||
<span class="synStatement">return</span> (<span class="synConstant">-1</span>) <span class="synStatement">if</span> (<span class="synIdentifier">$i</span> =~<span class="synStatement"> /</span><span class="synSpecial">[a-zA-Z]</span><span class="synStatement">/</span>);
|
||
}
|
||
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
}
|
||
</pre>
|
||
|
||
<p>
|
||
</p>
|
||
|
||
<p>
|
||
<center>
|
||
</center>
|
||
</p>
|
||
|
||
<div class="hatena-asin-detail">
|
||
<a href="http://www.amazon.co.jp/dp/4797336803/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4797336803/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51LwEY4XTiL._SL160_.jpg" class="hatena-asin-detail-image" alt="新版Perl言語プログラミングレッスン入門編" title="新版Perl言語プログラミングレッスン入門編" /></a></p>
|
||
|
||
<div class="hatena-asin-detail-info">
|
||
<p class="hatena-asin-detail-title">
|
||
<a href="http://www.amazon.co.jp/dp/4797336803/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4797336803/?tag=hatena_st1-22&ascsubtag=d-7ibv', '新版Perl言語プログラミングレッスン入門編');">新版Perl言語プログラミングレッスン入門編</a>
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%B7%EB%BE%EB%B9%C0" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B7%EB%BE%EB%B9%C0', '結城浩');" class="keyword">結城浩</a>
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%A5%BD%A5%D5%A5%C8%A5%D0%A5%F3%A5%AF%A5%AF%A5%EA%A5%A8%A5%A4%A5%C6%A5%A3%A5%D6" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%A5%BD%A5%D5%A5%C8%A5%D0%A5%F3%A5%AF%A5%AF%A5%EA%A5%A8%A5%A4%A5%C6%A5%A3%A5%D6', 'ソフトバンククリエイティブ');" class="keyword">ソフトバンククリエイティブ</a>
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">発売日:</span> 2006/10/21
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">メディア:</span> 単行本
|
||
</li>
|
||
<li>
|
||
<span class="hatena-asin-detail-label">購入</span>: 16人 <span class="hatena-asin-detail-label">クリック</span>: 235回
|
||
</li>
|
||
<li>
|
||
<a href="http://d.hatena.ne.jp/asin/4797336803" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4797336803', 'この商品を含むブログ (86件) を見る');" target="_blank">この商品を含むブログ (86件) を見る</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="hatena-asin-detail-foot">
|
||
</div>
|
||
</div>
|
||
</div>
|