Class: MyMatrix
- Inherits:
-
Object
- Object
- MyMatrix
- Includes:
- Enumerable
- Defined in:
- lib/mymatrix.rb,
lib/mymatrix/version.rb
Constant Summary collapse
- SEPARATOR =
to_t()の際のセパレータ。
"\t"
- VERSION =
"0.0.8"
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#internal_lf ⇒ Object
Returns the value of attribute internal_lf.
-
#mx ⇒ Object
Returns the value of attribute mx.
Class Method Summary collapse
-
.cp932ize(str) ⇒ Object
CP932範囲外の文字コードを変換する関数。ruby1.9の正規表現(鬼車)のため、1.8では使えない。.
- .make_path(path, opts = { }) ⇒ Object
-
.tosjis(str) ⇒ Object
Args str :: UTF8文字列 ====Return CP932の文字列.
-
.toutf8(str) ⇒ Object
- 外部ファイルエンコード(CP932)を内部エンコード(UTF8)に変換する ====Args str
-
CP932 string ====Return UTF8 String.
-
.toUtf8Mac(str) ⇒ Object
MacOSXのファイルシステムで使われるUTF8-Mac(BOMつきUTF)に変換する.
Instance Method Summary collapse
-
#+(other) ⇒ Object
未検証.
- #<<(row) ⇒ Object
- #[](i) ⇒ Object
- #[]=(key, value) ⇒ Object
- #addColumn(header, column) ⇒ Object
-
#addFlg(targetHeader, compareHeader, values, flgValue = '1') ⇒ Object
compareHeaderの値の中に、valuesに書かれた値があったら、targetHeaderにフラグを立てる.
-
#addHeader(key) ⇒ Object
ヘッダを追加する.
-
#addHeaders(aheaders) ⇒ Object
ヘッダを追加する(配列).
- #addRow(row) ⇒ Object
- #concat(mx, opt = {}) ⇒ Object
- #concatCells(headers, colname) ⇒ Object
-
#concatDir(dir) ⇒ Object
フォルダ内ファイルの結合。絶対パスを指定する.
- #concatFile(file, opt = {}) ⇒ Object
-
#correctCityCodes!(colname = '都道府県市区町村コード') ⇒ Object
都道府県市区町村コードの桁そろえ Excelで先頭の0が落ちて桁が変わるため.
- #count(header, value) ⇒ Object
-
#countup(header) ⇒ Object
全件カウントして、[value, count] という配列に格納する.
-
#cutOff(head, num) ⇒ Object
num文字以上の項目をnum文字に丸める.
-
#delEndSp! ⇒ Object
末尾空白の削除.
- #delete(v) ⇒ Object
- #delete_at(pos) ⇒ Object
-
#delete_if ⇒ Object
未検証。「要素を順番にブロックに渡して評価し、その結果が真になった要素を すべて削除します。」.
-
#devide(n) ⇒ Object
n分割した配列を返却する.
-
#divide(splitNum) ⇒ Object
複数に分割されたテキストファイルを出力する.
- #dupRow(row, destmx, destrow, headers) ⇒ Object
- #each ⇒ Object
-
#empty ⇒ Object
行が空(ヘッダはそのまま)のコピーを返却する.
-
#empty! ⇒ Object
selfの行を空にする.
- #empty? ⇒ Boolean
- #fill(rows) ⇒ Object
- #filter(header, value) ⇒ Object
-
#find ⇒ Object
ブロックがTrueになる、配列(参照)を返却するメソッド.
-
#first ⇒ Object
Arrayっぽく使うためのメソッド。内部の@mx.first.
-
#flushCol(colname) ⇒ Object
def concat!(mx) o = self.concat(mx) self = o return self end def concatFile!(file) o = self.concatFile(file) self = o return self end.
-
#getColumn(colName) ⇒ Object
カラムの値を配列で返却する.
-
#getColumns(colNames) ⇒ Object
カラムの値を複数指定して、多次元配列を返却するメソッド。.
- #getColumnsByMatrix(colNames) ⇒ Object
-
#getCrrValue(row, str) ⇒ Object
MyipcMatrixとの互換性のため。getValueのエイリアス.
- #getDoubles(arr) ⇒ Object
-
#getHeaders ⇒ Object
ヘッダのコピーを返却する.
-
#getPath ⇒ Object
読み込んだファイルのパスを返却する.
- #getValue(row, str) ⇒ Object (also: #val)
-
#getValues(colName) ⇒ Object
getColumn のエイリアスメソッド.
-
#index(colName, value) ⇒ Object
colNameの値がvalueの行のうち先頭のものを返却する。todo:名称がよくない。.
-
#initialize(file = nil, opts = {}) ⇒ MyMatrix
constructor
- コンストラクタ ====Args file
-
オープンするファイル。 ====Return 生成されたMyMatrixオブジェクト.
-
#last ⇒ Object
Arrayっぽく使うためのメソッド。内部の@mx.last.
-
#localEncode(v, enc = 's') ⇒ Object
ファイルの中身をSJISにするために使ってる.
-
#makeHash(fromColName, toColName) ⇒ Object
fromColName => toColNameのハッシュを作成する。 fromColNameの値が同じだとtoColNameが上書きされるので使いにくいと思われる。.
-
#makeKey(colname) ⇒ Object
colnameがキーのハッシュを作る。valueはrowの配列。.
- #myescape(cell) ⇒ Object
- #pop ⇒ Object
- #push(var) ⇒ Object
- #pushColumn(header, column) ⇒ Object
-
#replaceHeader(before, after) ⇒ Object
ヘッダを置き換える.
-
#reverse ⇒ Object
未検証.
-
#search(colName, value) ⇒ Object
colnameの値がvalueの行のrowを配列で返却する。todo:名称がよくない。.
-
#searchHeader(str) ⇒ Object
strを含む(/#str/)ヘッダ名を配列で返却する。.
-
#searchIndexes(colName, value) ⇒ Object
colnameの値がvalueの行のもののインデックス値を配列で返却する。todo:名称がよくない。.
-
#select(headers) ⇒ Object
headersに記載の.
-
#setPath(path) ⇒ Object
ファイルパスを設定する。 to_tを引数なしで使うと、設定したパスにファイルが生成される。.
- #setSame(head, headValue, hash) ⇒ Object
-
#setValue(row, str, value) ⇒ Object
def getValues(row, arr) out = [] arr.each do |ele| out << getValue(row, ele) end if(out.size == 0) out = nil end return out end.
- #shift ⇒ Object
- #shiftColumn ⇒ Object
-
#size ⇒ Object
行数を返却する.
- #sortBy(colname, reverse = false) ⇒ Object
-
#to_ary ⇒ Object
配列と引き算とかする際に使われる。.
-
#to_csv(outFile) ⇒ Object
CSV出力する。ダブルクオーテーションやカンマをエスケープする。.
- #to_s ⇒ Object
- #to_s_with_header ⇒ Object
-
#to_t(outFile = nil, opts = {}) ⇒ Object
テキスト出力する.
-
#to_t_with(postfix = "out", opts = {}) ⇒ Object
読み込んだファイル名にpostfixを付与してテキストファイル出力する.
-
#to_text(outFile) ⇒ Object
使い方はto_t()を参照。yield。.
-
#twoByteKana! ⇒ Object
半角カタカナの全角化.
-
#uniq! ⇒ Object
未検証。「要素の重複判定は、Object#eql? により行われます。」www.ruby-lang.org/ja/old-man/html/Array.html#uniq.
- #unshift(var) ⇒ Object
-
#unShiftColumn(header, column) ⇒ Object
使い勝手が良くないので気をつけて使う(todo参照).
-
#with_serial(headerName = 'No.') ⇒ Object
行番号を付与する.
- #without(regexp_or_string) ⇒ Object
Constructor Details
#initialize(file = nil, opts = {}) ⇒ MyMatrix
コンストラクタ
Args
- file
-
オープンするファイル。
Return
生成されたMyMatrixオブジェクト
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mymatrix.rb', line 30 def initialize(file=nil, opts={}) #内部改行コード。 @internal_lf = '<br>' rnd = rand(9999) i = 0 begin @log = Logger.new("MyMatrix_ERR_#{i}.log") rescue i += 1 retry end @log.level = Logger::DEBUG @file = file @mx = LoaderFactory.load(@file, opts) @headers = @mx.shift registerMatrix return self end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
20 21 22 |
# File 'lib/mymatrix.rb', line 20 def file @file end |
#internal_lf ⇒ Object
Returns the value of attribute internal_lf.
20 21 22 |
# File 'lib/mymatrix.rb', line 20 def internal_lf @internal_lf end |
#mx ⇒ Object
Returns the value of attribute mx.
20 21 22 |
# File 'lib/mymatrix.rb', line 20 def mx @mx end |
Class Method Details
.cp932ize(str) ⇒ Object
CP932範囲外の文字コードを変換する関数。ruby1.9の正規表現(鬼車)のため、1.8では使えない。
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
# File 'lib/mymatrix.rb', line 950 def self.cp932ize(str) out = str.dup cases = [ #['−', '―'], #MINUS SIGN(U+2212) to FULLWIDTH HYPHEN-MINUS(U+2015)(windows) #↑仕様としては上記が正しいが、運用上MINUS SIGN(U+2212) は FULLWIDTH HYPHEN-MINUS(U+FF0D)に変換する #キー入力時にMacとWindowsで同じ文字コードとなることが望ましいため。 ['〜','~'], #WAVE DASH (U+301C) to FULLWIDTH TILDE(U+FF5E)(windows) ['‖','∥'], #DOUBLE VERTICAL LINE (U+2016, "‖") を PARALLEL TO (U+2225, "∥") に ['—', '―'], #EM DASH (U+2014, "—") を HORIZONTAL BAR (U+2015, "―") に #以下、キー入力を想定した変換。 ['ー', 'ー'], #MacのハイフンF7(google ime)→Windows(googleime):同じ ['ー', 'ー'], #MacのハイフンF8(google ime)→Windows(googleime):同じ ['−', '-'], #MacのハイフンF9[−](google ime)→Windows[-](googleime):違う。MINUS SIGN(U+2212) to FULLWIDTH HYPHEN-MINUS(U+FF0D) ['-', '-'], #MacのハイフンF10(google ime)→Windows(googleime):同じ #ユニコード固有文字:ノーブレークスペース ['[\u00A0]', ' '], #yen ['[\u00A5]', '¥'], # éとè:eの上に´と` ['[\u00E9]', 'e'],['[\u00E8]', 'e'], # todo:よく使う文字(http://www.geocities.jp/laut6/mojibakesetumei/mojibakesetumei2.html より) #spaces ['[\u2000]', ' '],['[\u2001]', ' '],['[\u2002]', ' '],['[\u2003]', ' '],['[\u2004]', ' '],['[\u2005]', ' '],['[\u2006]', ' '],['[\u2007]', ' '],['[\u2008]', ' '],['[\u2009]', ' '],['[\u200A]', ' '],['[\u205F]', ' '], #Japanese Addresses ['鵢崎', 'みさ崎'], ['繫', '繋'], ['萊', '莱'] ] cases.each do |c| out.gsub!(/#{c[0]}/, c[1]) end return out end |
.make_path(path, opts = { }) ⇒ Object
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'lib/mymatrix.rb', line 1041 def self.make_path(path, opts={ }) # opath = makepath(@file, {:ext=>nil, :postfix=>postfix}) dir = File.dirname(path) ext = opts[:ext] ext ||= File.extname(path) postfix = opts[:postfix].to_s basename = File.basename(path, ".*") opath = (FileIO.encodePath("#{dir}/#{basename}_#{postfix}#{ext}")) end |
.tosjis(str) ⇒ Object
Args
- str
-
UTF8文字列
Return
CP932の文字列
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mymatrix.rb', line 58 def self.tosjis(str) if(RUBY_VERSION =~ /1\.[^9]/) #-xは半角カナを全角にするのを抑止するオプション。 out = NKF.nkf('-W -x -s --cp932', str) else str = MyMatrix.cp932ize(str) begin out = str.encode("Windows-31J") rescue => e p e p str out = str end end return out end |
.toutf8(str) ⇒ Object
外部ファイルエンコード(CP932)を内部エンコード(UTF8)に変換する
Args
- str
-
CP932 string
Return
UTF8 String
79 80 81 82 83 84 85 86 87 |
# File 'lib/mymatrix.rb', line 79 def self.toutf8(str) #入力がShift-jisであるとする。 if(RUBY_VERSION =~ /1\.[^9]/) out = NKF.nkf('-S -x -w --cp932', str) else out = str.encode('UTF-8') end return out end |
.toUtf8Mac(str) ⇒ Object
MacOSXのファイルシステムで使われるUTF8-Mac(BOMつきUTF)に変換する
90 91 92 93 |
# File 'lib/mymatrix.rb', line 90 def self.toUtf8Mac(str) out = str return out end |
Instance Method Details
#+(other) ⇒ Object
未検証
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/mymatrix.rb', line 264 def +(other) out = MyipcMatrix.new othHeaders = other.getHeaders selHeaders = getHeaders selHeaders.each do |header| out.addColumn(header, getColumn(header)) end othHeaders.each do |header| out.addColumn(header, other.getColumn(header)) end return out end |
#<<(row) ⇒ Object
284 285 286 |
# File 'lib/mymatrix.rb', line 284 def <<(row) addRow(row) end |
#[](i) ⇒ Object
255 256 257 |
# File 'lib/mymatrix.rb', line 255 def [](i,j) return @mx[i][j] end |
#[]=(key, value) ⇒ Object
311 312 313 |
# File 'lib/mymatrix.rb', line 311 def []=(key, value) @mx[key] = value end |
#addColumn(header, column) ⇒ Object
281 282 283 |
# File 'lib/mymatrix.rb', line 281 def addColumn(header, column) pushColumn(header, column) end |
#addFlg(targetHeader, compareHeader, values, flgValue = '1') ⇒ Object
compareHeaderの値の中に、valuesに書かれた値があったら、targetHeaderにフラグを立てる
686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/mymatrix.rb', line 686 def addFlg(targetHeader, compareHeader, values, flgValue='1') compares = getColumn(compareHeader) values.each do|value| i = compares.index(value) if(i) setValue(@mx[i], targetHeader, flgValue) else #raise "VALUE NOT FOUND:#{value}" end end end |
#addHeader(key) ⇒ Object
ヘッダを追加する
544 545 546 |
# File 'lib/mymatrix.rb', line 544 def addHeader(key) addHeaders([key]) end |
#addHeaders(aheaders) ⇒ Object
ヘッダを追加する(配列)
537 538 539 540 541 |
# File 'lib/mymatrix.rb', line 537 def addHeaders(aheaders) @headers.concat(aheaders).uniq! registerMatrix end |
#addRow(row) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/mymatrix.rb', line 287 def addRow(row) if(row.class != Array) row = [row] end row.size.times do |i| if(row[i] == nil) row[i] = '' end end headerSize = getHeaders.size rowSize = row.size if(headerSize > rowSize) (headerSize - rowSize).times do |i| row << '' end elsif(rowSize > headerSize) raise("row size error. headerSize:#{headerSize} rowSize:#{rowSize}") end @mx << row.dup end |
#concat(mx, opt = {}) ⇒ Object
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'lib/mymatrix.rb', line 804 def concat(mx, opt={}) notfoundHeaders = [] mx.each do |row| o = [] mx.getHeaders.each do |head| begin self.setValue(o, head, mx.getValue(row, head)) rescue => e #p e if(opt[:loose]==true) self.addHeaders([head]) #p "#{head} added" retry else notfoundHeaders << head end end end self << o end if(notfoundHeaders.size > 0) raise "notfoundHeader : #{notfoundHeaders.uniq.join(',')}" end return self end |
#concatCells(headers, colname) ⇒ Object
637 638 639 640 641 642 643 644 645 646 |
# File 'lib/mymatrix.rb', line 637 def concatCells(headers, colname) addHeaders([colname]) @mx.each do |row| val = [] headers.each do |header| val << getValue(row, header) end setValue(row, colname, val.join('_').gsub(/_+/, '_')) end end |
#concatDir(dir) ⇒ Object
フォルダ内ファイルの結合。絶対パスを指定する
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
# File 'lib/mymatrix.rb', line 836 def concatDir(dir) dir = File.(dir) Dir.entries(dir).each do |ent| if(ent =~ /^\./) else #p ent file = dir + '/' + ent #p "concat:#{file}" nmx = MyMatrix.new(file) self.concat(nmx) end end end |
#concatFile(file, opt = {}) ⇒ Object
829 830 831 832 833 834 |
# File 'lib/mymatrix.rb', line 829 def concatFile(file, opt={}) #p file mx = MyMatrix.new(file) self.concat(mx, opt) return self end |
#correctCityCodes!(colname = '都道府県市区町村コード') ⇒ Object
都道府県市区町村コードの桁そろえ Excelで先頭の0が落ちて桁が変わるため
900 901 902 903 904 905 906 907 908 909 910 911 912 |
# File 'lib/mymatrix.rb', line 900 def correctCityCodes!(colname = '都道府県市区町村コード') self.each do |row| code = self.val(row, colname) if(code.length == 4) self.setValue(row, colname, sprintf("%05d", code)) elsif(code.length == 5) #correct else raise "Citycode length error. '#{code}'" end end return self end |
#count(header, value) ⇒ Object
747 748 749 750 751 752 753 754 755 756 |
# File 'lib/mymatrix.rb', line 747 def count(header, value) out = 0 arr = getColumn(header) arr.each do |ele| if(ele =~ /#{value}/) out += 1 end end return out end |
#countup(header) ⇒ Object
全件カウントして、[value, count] という配列に格納する
758 759 760 761 762 763 764 765 |
# File 'lib/mymatrix.rb', line 758 def countup(header) out = [] values = getColumn(header).uniq values.each do |value| out << [value, self.count(header, value)] end return out end |
#cutOff(head, num) ⇒ Object
num文字以上の項目をnum文字に丸める
1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/mymatrix.rb', line 1033 def cutOff(head, num) self.each do |row| v = self.val(row, head) if(v =~ /(.{#{num}})/) self.setValue(row, head, $1) end end end |
#delEndSp! ⇒ Object
末尾空白の削除
915 916 917 918 919 920 921 922 923 924 925 |
# File 'lib/mymatrix.rb', line 915 def delEndSp! self.each do |row| self.getHeaders.each do |head| val = self.val(row, head) if(val =~ /(.*)[ ]$/) self.setValue(row, head, $1) end end end return self end |
#delete(v) ⇒ Object
581 582 583 |
# File 'lib/mymatrix.rb', line 581 def delete(v) @mx.delete(v) end |
#delete_at(pos) ⇒ Object
570 571 572 |
# File 'lib/mymatrix.rb', line 570 def delete_at(pos) @mx.delete_at(pos) end |
#delete_if ⇒ Object
未検証。「要素を順番にブロックに渡して評価し、その結果が真になった要素を すべて削除します。」
575 576 577 578 579 580 |
# File 'lib/mymatrix.rb', line 575 def delete_if out = @mx.delete_if do |row| yield(row) end @mx = out end |
#devide(n) ⇒ Object
n分割した配列を返却する
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'lib/mymatrix.rb', line 668 def devide(n) out = [] mx = @mx.dup eleSize = mx.size/n n.times do |i| o = self.empty eleSize.times do |j| o << mx.shift end out << o end #@mx.size%n分余ってるので、追加 mx.each do |ele| out[n-1] << ele end return out end |
#divide(splitNum) ⇒ Object
複数に分割されたテキストファイルを出力する
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/mymatrix.rb', line 350 def divide(splitNum) lineNum = (@mx.size / splitNum) + 1 mymxs = [] tmp = MyMatrix.new tmp.file = @file tmp.addHeaders(getHeaders) @mx.each_with_index do |row, i| tmp << row.dup if((i+1) % lineNum == 0) mymxs << tmp tmp = MyMatrix.new tmp.addHeaders(getHeaders) tmp.file = @file end end mymxs << tmp mymxs.each_with_index do |mymx, i| p i mymx.to_t_with("#{i}") end end |
#dupRow(row, destmx, destrow, headers) ⇒ Object
882 883 884 885 886 887 888 |
# File 'lib/mymatrix.rb', line 882 def dupRow(row, destmx, destrow, headers) headers.each do |head| val = self.getValue(row, head) destmx.setValue(destrow, head, val) end return destrow end |
#each ⇒ Object
236 237 238 239 240 |
# File 'lib/mymatrix.rb', line 236 def each @mx.each do |row| yield(row) end end |
#empty ⇒ Object
行が空(ヘッダはそのまま)のコピーを返却する
716 717 718 719 720 |
# File 'lib/mymatrix.rb', line 716 def empty out = self.dup out.empty! return out end |
#empty! ⇒ Object
selfの行を空にする
723 724 725 726 |
# File 'lib/mymatrix.rb', line 723 def empty! @mx = [] return self end |
#empty? ⇒ Boolean
1029 1030 1031 |
# File 'lib/mymatrix.rb', line 1029 def empty? @mx.empty? end |
#fill(rows) ⇒ Object
727 728 729 730 731 732 |
# File 'lib/mymatrix.rb', line 727 def fill(rows) rows.each do |row| self << row end return self end |
#filter(header, value) ⇒ Object
774 775 776 777 778 779 780 781 782 783 |
# File 'lib/mymatrix.rb', line 774 def filter(header, value) out = empty @mx.each do|row| v = getValue(row, header) if(v == value) out << row end end return out end |
#find ⇒ Object
ブロックがTrueになる、配列(参照)を返却するメソッド
585 586 587 588 589 590 591 592 593 594 |
# File 'lib/mymatrix.rb', line 585 def find #todo rowsを返却するのと、Mymatrxixを返却するのとどっちがイイのか。。 rows = [] @mx.each do |row| if(yield(row)) rows << row.dup end end return rows end |
#first ⇒ Object
Arrayっぽく使うためのメソッド。内部の@mx.first
1022 1023 1024 |
# File 'lib/mymatrix.rb', line 1022 def first @mx[0] end |
#flushCol(colname) ⇒ Object
def concat!(mx) o = self.concat(mx) self = o return self end def concatFile!(file) o = self.concatFile(file) self = o return self end
863 864 865 866 867 868 |
# File 'lib/mymatrix.rb', line 863 def flushCol(colname) @mx.each do |row| self.setValue(row, colname, '') end return self end |
#getColumn(colName) ⇒ Object
カラムの値を配列で返却する
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/mymatrix.rb', line 144 def getColumn(colName) out = [] @mx.each do |row| begin out << getValue(row, colName) rescue raise "#{colName} notfound: #{row}" end end return out end |
#getColumns(colNames) ⇒ Object
カラムの値を複数指定して、多次元配列を返却するメソッド。
161 162 163 164 165 166 167 |
# File 'lib/mymatrix.rb', line 161 def getColumns(colNames) out = [] colNames.each do |colName| out << getColumn(colName) end return out end |
#getColumnsByMatrix(colNames) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/mymatrix.rb', line 170 def getColumnsByMatrix(colNames) out = MyMatrix.new colNames.each do |colName| col = getColumn(colName) out.addColumn(colName, col) end return out end |
#getCrrValue(row, str) ⇒ Object
MyipcMatrixとの互換性のため。getValueのエイリアス
632 633 634 635 |
# File 'lib/mymatrix.rb', line 632 def getCrrValue(row, str) p 'this class is not MyipcMatrix.' getValue(row, str) end |
#getDoubles(arr) ⇒ Object
766 767 768 769 770 771 772 |
# File 'lib/mymatrix.rb', line 766 def getDoubles(arr) doubles = arr.select do |e| arr.index(e) != arr.rindex(e) end doubles.uniq! return doubles end |
#getHeaders ⇒ Object
ヘッダのコピーを返却する
486 487 488 489 |
# File 'lib/mymatrix.rb', line 486 def getHeaders out = @headers.dup return out end |
#getPath ⇒ Object
読み込んだファイルのパスを返却する
649 650 651 |
# File 'lib/mymatrix.rb', line 649 def getPath return @file end |
#getValue(row, str) ⇒ Object Also known as: val
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mymatrix.rb', line 182 def getValue(row, str) out = nil index = @headerH[str] if(index) out = row[index] #お尻のセルでNULLの場合などは、nilが返却されてしまう。なので、''となるようにする。 if(!out) out = '' end #参照を渡さないためdupする out = out.dup else raise "header not found:#{str} file:#{@file}" end return out end |
#getValues(colName) ⇒ Object
getColumn のエイリアスメソッド
157 158 159 |
# File 'lib/mymatrix.rb', line 157 def getValues(colName) return getColumn(colName) end |
#index(colName, value) ⇒ Object
colNameの値がvalueの行のうち先頭のものを返却する。todo:名称がよくない。
497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/mymatrix.rb', line 497 def index(colName, value) out = nil col = getColumn(colName) col.each_with_index do |cell, i| if(value == cell) out = i break end end return out end |
#last ⇒ Object
Arrayっぽく使うためのメソッド。内部の@mx.last
1026 1027 1028 |
# File 'lib/mymatrix.rb', line 1026 def last out = @mx[self.size-1] end |
#localEncode(v, enc = 's') ⇒ Object
ファイルの中身をSJISにするために使ってる
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/mymatrix.rb', line 372 def localEncode(v, enc = 's') case enc when 'u' str = MyMatrix.toutf8(v) when 's' str = MyMatrix.tosjis(v) else str = MyMatrix.tosjis(v) end end |
#makeHash(fromColName, toColName) ⇒ Object
fromColName => toColNameのハッシュを作成する。 fromColNameの値が同じだとtoColNameが上書きされるので使いにくいと思われる。
608 609 610 611 612 613 614 615 616 |
# File 'lib/mymatrix.rb', line 608 def makeHash(fromColName, toColName) out = Hash.new @mx.each do |row| from = getValue(row, fromColName) to = getValue(row, toColName) out[from] = to end return out end |
#makeKey(colname) ⇒ Object
colnameがキーのハッシュを作る。valueはrowの配列。
619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/mymatrix.rb', line 619 def makeKey(colname) out = {} self.each do |row| key = self.val(row, colname) if(out[key] == nil) out[key] = [] end out[key] << row end return out end |
#myescape(cell) ⇒ Object
464 465 466 467 468 469 470 471 472 |
# File 'lib/mymatrix.rb', line 464 def myescape(cell) o = cell.to_s.dup o.gsub!(/"/, '""') if o =~ /[",']/ #' o = "\"#{o}\"" end return o end |
#pop ⇒ Object
564 565 566 |
# File 'lib/mymatrix.rb', line 564 def pop return @mx.pop end |
#push(var) ⇒ Object
567 568 569 |
# File 'lib/mymatrix.rb', line 567 def push(var) return @mx.push(var) end |
#pushColumn(header, column) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/mymatrix.rb', line 315 def pushColumn(header, column) colPos = @headers.length @headers << header registerMatrix column.each_with_index do |cell, i| if(@mx[i] == nil) @mx[i] = [] end @mx[i][colPos] = cell end end |
#replaceHeader(before, after) ⇒ Object
ヘッダを置き換える
491 492 493 494 |
# File 'lib/mymatrix.rb', line 491 def replaceHeader(before, after) @headers[@headerH[before]] = after registerMatrix end |
#reverse ⇒ Object
未検証
242 243 244 245 246 247 248 249 |
# File 'lib/mymatrix.rb', line 242 def reverse out = empty @mx.reverse.each do |row| out << row end return out end |
#search(colName, value) ⇒ Object
colnameの値がvalueの行のrowを配列で返却する。todo:名称がよくない。
522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/mymatrix.rb', line 522 def search(colName, value) indexes = [] col = getColumn(colName) col.each_with_index do |cell, i| if(value == cell) indexes << i end end out = self.empty indexes.each do |index| out << @mx[index] end return out end |
#searchHeader(str) ⇒ Object
strを含む(/#str/)ヘッダ名を配列で返却する。
658 659 660 661 662 663 664 665 |
# File 'lib/mymatrix.rb', line 658 def searchHeader(str) out = [] getHeaders.each do |header| if(header =~ /#{str}/) out << header end end end |
#searchIndexes(colName, value) ⇒ Object
colnameの値がvalueの行のもののインデックス値を配列で返却する。todo:名称がよくない。
510 511 512 513 514 515 516 517 518 519 |
# File 'lib/mymatrix.rb', line 510 def searchIndexes(colName, value) out = [] col = getColumn(colName) col.each_with_index do |cell, i| if(value == cell) out << i end end return out end |
#select(headers) ⇒ Object
headersに記載の
597 598 599 600 601 602 603 604 |
# File 'lib/mymatrix.rb', line 597 def select(headers) out = self.class.new headers.each do |header| out.addColumn(header, getColumn(header)) end out.file = @file return out end |
#setPath(path) ⇒ Object
ファイルパスを設定する。 to_tを引数なしで使うと、設定したパスにファイルが生成される。
653 654 655 |
# File 'lib/mymatrix.rb', line 653 def setPath(path) @file = path end |
#setSame(head, headValue, hash) ⇒ Object
889 890 891 892 893 894 895 896 |
# File 'lib/mymatrix.rb', line 889 def setSame(head, headValue, hash) idxs = self.searchIndexes(head, headValue) idxs.each do |idx| hash.each_pair do |key, value| self.setValue(@mx[idx], key, value) end end end |
#setValue(row, str, value) ⇒ Object
def getValues(row, arr) out = [] arr.each do |ele| out << getValue(row, ele) end if(out.size == 0) out = nil end return out end
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/mymatrix.rb', line 214 def setValue(row, str, value) if(!row) raise 'row is nil' end index = @headerH[str] if(!index) addHeaders([str]) end #参照先の値も変更できるように、破壊メソッドを使う。row[@headerH[str]] = valueでは、参照先が切り替わってしまうので、値の置き換えにならない。 #findなどで取得したrowに対して処理を行う際に必要な変更。 if(row[@headerH[str]].class == String) row[@headerH[str]].sub!(/^.*$/, value) else #raise('not string error.') #todo 強烈なバグな気もするが、例外を回避し値を代入2010年12月15日 begin row[@headerH[str]] = value.to_s rescue row[@headerH[str]] = '' end end end |
#shift ⇒ Object
558 559 560 |
# File 'lib/mymatrix.rb', line 558 def shift return @mx.shift end |
#shiftColumn ⇒ Object
339 340 341 342 343 344 345 346 347 |
# File 'lib/mymatrix.rb', line 339 def shiftColumn() header = @headers.shift column = [] registerMatrix @mx.each do |row| column << row.shift end return header, column end |
#size ⇒ Object
行数を返却する
549 550 551 |
# File 'lib/mymatrix.rb', line 549 def size return @mx.size end |
#sortBy(colname, reverse = false) ⇒ Object
869 870 871 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/mymatrix.rb', line 869 def sortBy(colname, reverse=false) sortmx = [] self.each do |row| key = self.getValue(row, colname) sortmx << [key, row] end sortmx.sort! self.empty! sortmx.each do |keyrow| self << keyrow[1] end return self end |
#to_ary ⇒ Object
配列と引き算とかする際に使われる。
785 786 787 788 789 790 791 792 |
# File 'lib/mymatrix.rb', line 785 def to_ary arr = [] @mx.each do |row| #arr << row.dup arr << row end return arr end |
#to_csv(outFile) ⇒ Object
CSV出力する。ダブルクオーテーションやカンマをエスケープする。
481 482 483 |
# File 'lib/mymatrix.rb', line 481 def to_csv(outFile) to_t(outFile, {:separator=>',', :escape=>true}) end |
#to_s ⇒ Object
793 794 795 796 797 798 799 |
# File 'lib/mymatrix.rb', line 793 def to_s out = '' @mx.each do |row| out = out + row.to_s + "\n" end return out end |
#to_s_with_header ⇒ Object
800 801 802 803 |
# File 'lib/mymatrix.rb', line 800 def to_s_with_header out = self.getHeaders.to_s + "\n" out = out + self.to_s end |
#to_t(outFile = nil, opts = {}) ⇒ Object
テキスト出力する
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/mymatrix.rb', line 411 def to_t(outFile=nil, opts={}) if(!outFile) outFile = @file end #拡張子の判別 ext = File.extname(outFile).downcase case ext when '.csv' opts[:separator] ||= ',' opts[:escape] ||= true when '.xls' p 'use Tab-Separated-Value text format.' outFile = outFile + '.txt' when '.xlsx' p 'use Tab-Separated-Value text format.' outFile = outFile + '.txt' else #do nothing end #デフォルトオプションの設定 opts[:enc] ||= 's' opts[:escape] ||= false opts[:separator] ||= SEPARATOR to_text(outFile) do |row| orow = [] if(opts[:escape]) row.each do |cell| orow << myescape(cell) end else row.each do |cell| orow << cell.to_s.gsub(/[#{opts[:separator]}\r\n]/, '') end end begin str = localEncode(orow.join(opts[:separator]), opts[:enc]) rescue Encoding::UndefinedConversionError orow.each do |ele| begin localEncode(ele, opts[:enc]) rescue raise "encode error.#{ele}\n(#{orow})" end end @log.debug(row.join(opts[:separator])) end str end end |
#to_t_with(postfix = "out", opts = {}) ⇒ Object
読み込んだファイル名にpostfixを付与してテキストファイル出力する
474 475 476 477 478 |
# File 'lib/mymatrix.rb', line 474 def to_t_with(postfix="out", opts={}) opath = MyMatrix.make_path(@file, {:ext=>nil, :postfix=>postfix}) to_t(opath, opts) return opath end |
#to_text(outFile) ⇒ Object
使い方はto_t()を参照。yield。
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/mymatrix.rb', line 383 def to_text(outFile) outFile = FileIO.encodePath(outFile) out = [] out << @headers @mx.each do |row| out << row end begin fo = open(outFile, 'wb') rescue => e p "cannot write file...#{outFile}" p e sleep(5) retry end out.each_with_index do |row, i| if(row == nil) warn("line #{i} is nil") fo.print("") else str = yield(row) fo.print(str) end fo.print("\r\n") end fo.close end |
#twoByteKana! ⇒ Object
半角カタカナの全角化
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
# File 'lib/mymatrix.rb', line 927 def twoByteKana! self.each do |row| self.getHeaders.each do |head| val = self.val(row, head) #if(val =~ /[-~]/) if(val =~ /[-]/) #p "#{self.file} #{val}" next end #nval = Kconv.kconv(val, Kconv::UTF8, Kconv::UTF8) #nval = NKF.nkf('-W -w -x --cp932', val) nval = NKF.nkf('-W -w', val) if(val!=nval) #p "#{val}=>#{nval}" end self.setValue(row, head, nval) end end return self end |
#uniq! ⇒ Object
未検証。「要素の重複判定は、Object#eql? により行われます。」www.ruby-lang.org/ja/old-man/html/Array.html#uniq
554 555 556 |
# File 'lib/mymatrix.rb', line 554 def uniq! @mx.uniq! end |
#unshift(var) ⇒ Object
561 562 563 |
# File 'lib/mymatrix.rb', line 561 def unshift(var) return @mx.unshift(var) end |
#unShiftColumn(header, column) ⇒ Object
使い勝手が良くないので気をつけて使う(todo参照)
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/mymatrix.rb', line 327 def unShiftColumn(header, column) @headers.unshift(header) registerMatrix column.each_with_index do |cell, i| if(@mx[i] == nil) @mx[i] = [] end #todo:ヘッダよりでかいrowがある場合バグる。期待していない一番右の値が取れてしまう。 @mx[i].unshift(cell) end end |
#with_serial(headerName = 'No.') ⇒ Object
行番号を付与する
735 736 737 738 739 740 741 742 743 744 |
# File 'lib/mymatrix.rb', line 735 def with_serial(headerName = 'No.') out = self.empty out.addHeaders([headerName], 1) self.each_with_index do |row, i| no = i + 1 newRow = [no].concat(row) out << newRow end return out end |
#without(regexp_or_string) ⇒ Object
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/mymatrix.rb', line 698 def without(regexp_or_string) if(regexp_or_string.class == String) regexp = /#{regexp_or_string}/ else regexp = regexp_or_string end newHeaders = [] @headers.each do |header| if(header =~ regexp) else newHeaders << header end end out = select(newHeaders) return out end |