回答編集履歴

2

追加

2017/06/13 06:59

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -87,3 +87,179 @@
87
87
  $str = encode('UTF-8', $str);
88
88
 
89
89
  ```
90
+
91
+
92
+
93
+ サンプルを添付しました。UTF8の場合
94
+
95
+ ```perl
96
+
97
+ use Encode 'decode';
98
+
99
+ use Encode 'encode';
100
+
101
+ @species1 = ();
102
+
103
+ @species2 = ();
104
+
105
+
106
+
107
+ #ファイルから読み込み
108
+
109
+ while ($line = <>) {
110
+
111
+
112
+
113
+ chomp $line;
114
+
115
+ $line = decode('UTF-8', $line);
116
+
117
+ ($sp1, $sp2) = split (/\s+/, $line);
118
+
119
+
120
+
121
+ print encode('UTF-8',$sp1), "\n"; #ここで問題発覚
122
+
123
+ push @species1, $sp1;
124
+
125
+ push @species2, $sp2;
126
+
127
+ }
128
+
129
+
130
+
131
+
132
+
133
+ ($ref_common, $ref_only_in1, $ref_only_in2) = &find_common(\@species1, \@species2);
134
+
135
+
136
+
137
+ print "common species\n";
138
+
139
+ foreach $sp (@$ref_common) {
140
+
141
+ $sp = encode('UTF-8', $sp);
142
+
143
+ print $sp, "\n";
144
+
145
+ }
146
+
147
+ print "\n";
148
+
149
+
150
+
151
+ print "only in the first group\n";
152
+
153
+ foreach $sp (@$ref_only_in1) {
154
+
155
+ $sp = encode('UTF-8', $sp);
156
+
157
+ print $sp, "\n"
158
+
159
+ }
160
+
161
+
162
+
163
+ print "\n";
164
+
165
+
166
+
167
+ print "only in the second group\n";
168
+
169
+ foreach $sp (@$ref_only_in2) {
170
+
171
+ $sp = encode('UTF-8', $sp);
172
+
173
+ print $sp, "\n";
174
+
175
+ }
176
+
177
+
178
+
179
+ sub find_common
180
+
181
+ {
182
+
183
+ my ($ref1, $ref2) = @_;
184
+
185
+
186
+
187
+ my @common = ();
188
+
189
+ my @only_in1 = ();
190
+
191
+ my @only_in2 = ();
192
+
193
+
194
+
195
+ my $found;
196
+
197
+
198
+
199
+ foreach my $sp1 (@$ref1) {
200
+
201
+ foreach my $sp2 (@$ref2) {
202
+
203
+ $found = 0;
204
+
205
+ if ($sp1 eq $sp2) {
206
+
207
+ $found = 1;
208
+
209
+ last;
210
+
211
+ }
212
+
213
+ }
214
+
215
+
216
+
217
+ if ($found == 1) {
218
+
219
+ push @common, $sp1;
220
+
221
+ }
222
+
223
+ else {
224
+
225
+ push @only_in1, $sp1;
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ foreach my $sp2 (@$ref2) {
234
+
235
+ foreach my $sp_common (@common) {
236
+
237
+ $found = 0;
238
+
239
+ if ($sp2 eq $sp_common) {
240
+
241
+ $found = 1;
242
+
243
+ last;
244
+
245
+ }
246
+
247
+ }
248
+
249
+ if ($found == 0) {
250
+
251
+ push @only_in2, $sp2;
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ return (\@common, \@only_in1, \@only_in2);
260
+
261
+ }
262
+
263
+ ```
264
+
265
+

1

追加

2017/06/13 06:59

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -57,3 +57,33 @@
57
57
  グンテ
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ perlには内部コードなるものが有りますので全角の文字列の場合変換されると良いです
64
+
65
+ 「UTF-8バイト文字列」を「内部文字列」に変換する
66
+
67
+ ```perl
68
+
69
+ use Encode 'decode';
70
+
71
+ # バイト文字列(外部からの入力)を内部文字列に変換($strがUTF-8の場合)
72
+
73
+ $str = decode('UTF-8', $str);
74
+
75
+ ```
76
+
77
+
78
+
79
+ 面倒ですが出力する場合は逆にします。
80
+
81
+ ```perl
82
+
83
+ use Encode 'encode';
84
+
85
+ # 内部文字列をUTF-8バイト文字列に変換する場合
86
+
87
+ $str = encode('UTF-8', $str);
88
+
89
+ ```