プロが教えるわが家の防犯対策術!

お世話になっております。
下記要件を満たす記述をご教授頂けますでしょうか。

<概要>
アンケートデータを結合してアウトプットします。

・アンケートデータ
1~10までの項目を当てはまるものだけ回答してます。
例)Aさん:1/5/6/8 
Bさん:3/4/7/8/9/10

・アウトプット形式
1~10のカラムがあり、回答された所を1とし、回答されてないものを0とする。
例)Aさん:1/0/0/0/1/1/0/1/0/0(1,5,6,8を1とし、それ以外を0とする)
Bさん:0/0/1/1/0/0/1/1/1/1(3,4,7,8,9,10を1とし、それ以外を0とする)


よろしくお願い致します。

A 回答 (4件)

#2のスクリプトの実行でエラーになったということですが、お使いになっている


perlはどういったものでしょうか。
わたしの手元で12.1と14.1で実行してなんの問題もありませんでした。

perl -v として実行するとどういった出力がされますか?


This is perl 5, version 14, subversion 1 (v5.14.1) built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Binary build 1401 [294969] provided by ActiveState http://www.ActiveState.com
Built Jun 16 2011 18:54:40

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
    • good
    • 0

まず、文字列 0/0/0/0/0/0/0/0/0/0 を作り、回答のあった項目を substr で1に変換しています。



use strict;
foreach my $ans ('1/5/6/8', '3/4/7/8/9/10') {
my $result = '0/0/0/0/0/0/0/0/0/0';
substr($result, ($_ - 1) * 2, 1) = 1 foreach split /\//, $ans;
print "$result\n";
}
    • good
    • 0

use strict;


use warnings;
use v5.12;

while (my $line = <DATA>) {
chomp $line;
my @ary = (0) x 10;
$ary[$_-1] = 1 for split '/', $line;

say join '/', @ary;
}

__END__
1/5/6/8
3/4/7/8/9/10

この回答への補足

ご回答ありがとうございます。
実行してみましたら下記エラーが発生致しました。

syntax error at xxx.pl line xxx, near "say join"

構文エラーだと検索してわかったのですが
perlを始めたばかりのため、対応方法まではわかりませんでした。

補足日時:2011/11/17 23:37
    • good
    • 0

しかるべくデータが入った配列を作ってください.

この回答への補足

ご回答ありがとうございます。

申し訳ございません、例を詳細に示せば宜しいのでしょうか?

補足日時:2011/11/17 23:40
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!