アプリ版:「スタンプのみでお礼する」機能のリリースについて

複数の種類のif else文のスクリプト結果を
まとめて表示する方法はあるのでしょうか?

たとえば「2000年」に対しての
データ(1)のif else文のデータ内容は「1月は猫 2月は犬・・」で
データ(2)のif else文は「1月は鳥 2月は申・・」であるとすると、

2000年の1月は「猫と鳥」と両方のif else文の結果を
表示させたい場合は、スクリプト的にはどのように記述すれば
よろしいでしょうか?

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

A 回答 (5件)

if 2000年1月


 データ(1)猫とデータ(2)鳥
elsif 2000年2月
 データ(1)犬とデータ(2)申
else
 その他

これではダメなのですか?
いまいちどうしたいのか理解しにくいため、
これくらいしか書けません。

違っていましたら補足してください。
ではでは☆

この回答への補足

ご回答ありがとうございます。
言葉不足で、スイマセン。
もういちど説明いたします。

2000年の1月2月3月・・12月まであるとします。これらの月に例えば、
その年月の天文暦の金星や火星の位置など、それぞれ独自の法則で
変化するデータを対応させる場合、金星のデータと火星のデータそれぞれの
if else文で分けて記述していくとします。

これらのそれぞれ条件のちがうif else文をひとつに統合させて表示させる
ことなど可能でしょうか?

つまり2000年1月の金星と2000年1月の火星のデータを
ひとつにまとめて表示させることはif else構文で出来ますでしょうか?

これでなんとなく伝わりましたでしょうか?
まだ分かりませんでしたら、お知らせ下さい。
よろしくお願いいたします。

補足日時:2001/01/28 18:41
    • good
    • 0

質問が出されたときから、何とか理解しようとしましたが、


どうも理解できなかったので、他の人が回答するのを待っていました。
で以下の回答と回答の補足を読んだのですが、
やっぱりわかりません(笑)

現在のif文がどうなっているのか教えてくれませんか?
つまり、金星のデータと水星のデータは、
何の条件で決まるんでしょう?

「2000年1月」が決まるだけで金星と水星の位置が決まるとしたら
Kanataさんのような書き方で十分だと思いますが、
そうじゃないっていうことですよね?

一つのif文にしたいというのは、
if文をandでつなげるだけで良いのでしょうか?

if(2000年1月 && 金星を出すその他の条件 && 水星を出すその他の条件 && ・・・)

どうもこれも違う気がします。
もう少し分かりやすい説明を希望します。

この回答への補足

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

すいません、スクリプト自体をアップします。
これら金星と火星の星座宮の年月日単位の変化です。

#以下1950年の1月~12月の金星のデータとする

if($in{'year'}==1950 && ($in{'month'}<=3 || ($in{'month'}==4 &&
$in{'day'}<=6))){ # ~4月の6日以下ならば
$horoscope = 'mizugame'; # みずがめ座

}elsif($in{'year'}==1950 && ($in{'month'}==4 || ($in{'month'}==5 &&
$in{'day'}<=5))){ # 4月の残り~5月の5日以下ならば
$horoscope = 'uo'; # うお座

}elsif($in{'year'}==1950 && ($in{'month'}==5 || ($in{'month'}==6 &&
$in{'day'}<=1))){ # 5月の残り~6月の1日以下ならば
$horoscope = 'ohituji'; # おひつじ座

}elsif($in{'year'}==1950 && ($in{'month'}==6 && $in{'day'}>=2 &&
$in{'day'}<=27)){ # 6月の残り~6月の27日以下ならば(6月2日以上かつ27日以下)
$horoscope = 'ousi'; # おうし座

}elsif($in{'year'}==1950 && (($in{'month'}==6 && $in{'day'}>=28) ||
($in{'month'}==7 && $in{'day'}<=22))){ # 6月の28日以上~7月の22日以下ならば
$horoscope = 'futago'; # ふたご座

}elsif($in{'year'}==1950 && ($in{'month'}==7 || ($in{'month'}==8 &&
$in{'day'}<=16))){ # 7月の残り~8月の16日以下ならば
$horoscope = 'kani'; # かに座

}elsif($in{'year'}==1950 && ($in{'month'}==8 || ($in{'month'}==9 &&
$in{'day'}<=9))){ # 8月の残り~9月の9日以下ならば
$horoscope = 'sisi'; # しし座

}elsif($in{'year'}==1950 && ($in{'month'}==9 || ($in{'month'}==10 &&
$in{'day'}<=4))){ # 9月の残り~10月の4日以下ならば
$horoscope = 'otome'; # おとめ座

}elsif($in{'year'}==1950 && ($in{'month'}==10 && $in{'day'}>=5 &&
$in{'day'}<=28)){ # 10月の残り~10月の28日以下ならば(10月5日以上かつ28日以下)
$horoscope = 'tennbinn'; # てんびん座

}elsif($in{'year'}==1950 && (($in{'month'}==10 && $in{'day'}>=29) ||
($in{'month'}==11 && $in{'day'}<=20))){ # 10月の29日以上~11月の20日以下ならば
$horoscope = 'sasori'; # さそり座

}elsif($in{'year'}==1950 && ($in{'month'}==11 || ($in{'month'}==12 &&
$in{'day'}<=14))){ # 11月の残り~12月の14日以下ならば
$horoscope = 'ite'; # いて座

}elsif($in{'year'}==1950 && ($in{'month'}==12 || ($in{'month'}==12 &&
$in{'day'}<=31))){ # 12月の残り~12月の31日以下ならば
$horoscope = 'yagi'; # やぎ座


#以下1950年の1月~12月の火星のデータとする

if($in{'year'}==1950 && ($in{'month'}==1 && $in{'day'}<=8)){
# ~1月の8日以下ならば
$horoscope = 'yagi';

}elsif($in{'year'}==1950 && ($in{'month'}==1)){ # 1月の残りならば
$horoscope = 'mizugame';

}elsif($in{'year'}==1950 && ($in{'month'}==2 || ($in{'month'}==2 &&
$in{'day'}<=24))){ # ~2月の24日以下ならば
$horoscope = 'uo';

}elsif($in{'year'}==1950 && ($in{'month'}==2 || ($in{'month'}==3 &&
$in{'day'}<=21))){ # 4月の残り~5月の5日以下ならば
$horoscope = 'ohituji';

}elsif($in{'year'}==1950 && ($in{'month'}==3 || ($in{'month'}==4 &&
$in{'day'}<=15))){ # 3月の残り~4月の15日以下ならば
$horoscope = 'ousi';

}elsif($in{'year'}==1950 && ($in{'month'}==4 || ($in{'month'}==5 &&
$in{'day'}<=10))){ # 4月の残り~5月の10日以下ならば
$horoscope = 'futago';

}elsif($in{'year'}==1950 && ($in{'month'}==5 || ($in{'month'}==6 &&
$in{'day'}<=7))){ # 5月の残り~6月の7日以下ならば
$horoscope = 'kani';

}elsif($in{'year'}==1950 && ($in{'month'}==6 || ($in{'month'}==7 &&
$in{'day'}<=8))){# 6月の残り~7月の8日以下ならば
$horoscope = 'sisi';

}elsif($in{'year'}==1950 && ($in{'month'}<=10 || ($in{'month'}==11 &&
$in{'day'}<=9))){# 7月の残り~11月の9日以下ならば
$horoscope = 'otome';

}elsif($in{'year'}==1950 && ($in{'month'}==11 || ($in{'month'}==12 &&
$in{'day'}<=7))){# 11月の残り~12月の7日以下ならば
$horoscope = 'tennbinn';

}elsif($in{'year'}==1950 && ($in{'month'}==12)){ # 12月の残りならば
$horoscope = 'sasori';


#例えば1950年1月1日ならば「金星はみずがめ、火星はやぎ」
というように両方とも表示できるようにしたいのですが・・

どうぞよろしくお願い致します。

補足日時:2001/01/29 19:11
    • good
    • 0

データと月の関係の法則が決まっているならば、if elseは使わないでもいいと思います。


質問を参考に、データを配列に入れておいて配列のindexでデータを表示してみました。
$month-1としているところを、適当な「独自の法則」の計算式におきかえれば、
if elseをいちいち書くよりもきっと簡単ですよ。
-------------------------------------------------------
@data1=('猫', '犬', );# 月ごとのデータ1
@data2=('鳥', '申', );# 月ごとのデータ2

$month=1;# 例えば1月。

print "データ1は$data1[$month-1]\n";
print "データ2は$data2[$month-1]\n";
-------------------------------------------------------

この回答への補足

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

せっかくご説明頂いたのに私の質問の仕方が悪かったため
イマイチ理解できません。

私がアップしましたif else文がどのように配列に置き換えることが
できるのか具体的にお見せ頂けますでしょうか?

大変お手数をおかけしますが、よろしくお願い致します。

補足日時:2001/01/29 19:14
    • good
    • 0

あまり単純な計算式にはできないようですね。


配列で……ってのは忘れてください。

んで、
> #例えば1950年1月1日ならば「金星はみずがめ、火星はやぎ」
と表示したいだけならば、
$horoscopeに代入しているところを、
例えば金星なら$venus、火星なら$marsに代入しておいて、
>print "「金星は$venus、火星は$mars」";
と出力すればよいのではないでしょうか。

判定する部分を関数に切り出してみました。(長くてすんません)
'しし'や'みずがめ'などを返すようにして、
そのまま表示に使えるようにしてあります。
老婆心ながら判定とコメントに矛盾があるところにコメントしてあります。
-------------------------------------------------------
#以下1950年の1月~12月の金星のデータとする
sub get_venus_data{
local($year,$month,$day)=@_;
local($horoscope)='undef';

if($year==1950){
if($month<=3 || ($month==4 && $day<=6)){ # ~4月の6日以下ならば
$horoscope = 'みずがめ'; # みずがめ座

}elsif($month==4 || ($month==5 && $day<=5)){ # 4月の残り~5月の5日以下ならば
$horoscope = 'うお'; # うお座

}elsif($month==5 || ($month==6 && $day<=1)){ # 5月の残り~6月の1日以下ならば
$horoscope = 'おひつじ'; # おひつじ座

}elsif($month==6 && $day<=27){ # 6月の残り~6月の27日以下ならば(6月2日以上かつ27日以下)
$horoscope = 'おうし'; # おうし座

}elsif($month==6 || ($month==7 && $day<=22)){ # 6月の28日以上~7月の22日以下ならば
$horoscope = 'ふたご'; # ふたご座

}elsif($month==7 || ($month==8 && $day<=16)){ # 7月の残り~8月の16日以下ならば
$horoscope = 'かに'; # かに座

}elsif($month==8 || ($month==9 && $day<=9)){ # 8月の残り~9月の9日以下ならば
$horoscope = 'しし'; # しし座

}elsif($month==9 || ($month==10 && $day<=4)){ # 9月の残り~10月の4日以下ならば
$horoscope = 'おとめ'; # おとめ座

}elsif($month==10 && $day<=28){ # 10月の残り~10月の28日以下ならば(10月5日以上かつ28日以下)
$horoscope = 'てんびん'; # てんびん座

}elsif($month==10 || ($month==11 && $day<=20)){ # 10月の29日以上~11月の20日以下ならば
$horoscope = 'さそり'; # さそり座

}elsif($month==11 || ($month==12 && $day<=14)){ # 11月の残り~12月の14日以下ならば
$horoscope = 'いて'; # いて座

}elsif($month==12){ # 12月の残り~12月の31日以下ならば
$horoscope = 'やぎ'; # やぎ座
}
}
return $horoscope;
}


#以下1950年の1月~12月の火星のデータとする

sub get_mars_data{
local($year,$month,$day)=@_;
local($horoscope)='undef';

if($year==1950){
if($month==1 && $day<=8){
# ~1月の8日以下ならば
$horoscope = 'やぎ';

}elsif($month==1){ # 1月の残りならば
$horoscope = 'みずがめ';

}elsif($month==2 && $day<=24){ # ~2月の24日以下ならば
$horoscope = 'うお';

}elsif($month==2 || ($month==3 && $day<=21)){ # 4月の残り~5月の5日以下ならば
# ☆コメントと判定が違ってるよ。
$horoscope = 'おひつじ';

}elsif($month==3 || ($month==4 && $day<=15)){ # 3月の残り~4月の15日以下ならば
$horoscope = 'おうし';

}elsif($month==4 || ($month==5 && $day<=10)){ # 4月の残り~5月の10日以下ならば
$horoscope = 'ふたご';

}elsif($month==5 || ($month==6 && $day<=7)){ # 5月の残り~6月の7日以下ならば
$horoscope = 'かに';

}elsif($month==6 || ($month==7 && $day<=8)){ # 6月の残り~7月の8日以下ならば
$horoscope = 'しし';

}elsif($month<=10 || ($month==11 && $day<=9)){ # 7月の残り~11月の9日以下ならば
# ☆コメントと判定が違ってるよ。
$horoscope = 'おとめ';

}elsif($month==11 || ($month==12 && $day<=7)){ # 11月の残り~12月の7日以下ならば
$horoscope = 'てんびん';

}elsif($month==12){ # 12月の残りならば
$horoscope = 'さそり';
}
}
return $horoscope;
}

# 入力から金星と火星の星座を求める。
$vinus = get_vinus_data($in{'year'}, $in{'month'}, $in{'day'});
$mars = get_mars_data($in{'year'}, $in{'month'}, $in{'day'});

print "金星は$vinus、火星は$mars";
-------------------------------------------------------

この回答への補足

ご丁寧なご説明を本当にありがとうございます。

早速教えて頂いた式を使わせていただきました。
htmlタグを埋め込んで表示させようとしたのですが、
うまく表示してくれません。

私の追加した文のどこがいけないのでしょうか?
たびたびお手数おかけしてしまって大変恐縮ですが
添削してやって下さい、よろしくお願いします。


#!/usr/local/bin/perl

# ↑(ローカルサーバーで試用中)
require 'cgi-lib.pl'; # ライブラリ読み込み。

&ReadParse; # フォーム取り込み。

#以下1950年の1月~12月の金星のデータとする
sub get_venus_data{
local($year,$month,$day)=@_;
local($horoscope)='undef';

if($year==1950){
if($month<=3 || ($month==4 && $day<=6)){ # ~4月の6日以下ならば
$horoscope = 'みずがめ'; # みずがめ座

}elsif($month==4 || ($month==5 && $day<=5)){ # 4月の残り~5月の5日以下ならば
$horoscope = 'うお'; # うお座

}elsif($month==5 || ($month==6 && $day<=1)){ # 5月の残り~6月の1日以下ならば
$horoscope = 'おひつじ'; # おひつじ座

}elsif($month==6 && $day<=27){ # 6月の残り~6月の27日以下ならば(6月2日以上かつ27日以下)
$horoscope = 'おうし'; # おうし座

}elsif($month==6 || ($month==7 && $day<=22)){ # 6月の28日以上~7月の22日以下ならば
$horoscope = 'ふたご'; # ふたご座

}elsif($month==7 || ($month==8 && $day<=16)){ # 7月の残り~8月の16日以下ならば
$horoscope = 'かに'; # かに座

}elsif($month==8 || ($month==9 && $day<=9)){ # 8月の残り~9月の9日以下ならば
$horoscope = 'しし'; # しし座

}elsif($month==9 || ($month==10 && $day<=4)){ # 9月の残り~10月の4日以下ならば
$horoscope = 'おとめ'; # おとめ座

}elsif($month==10 && $day<=28){ # 10月の残り~10月の28日以下ならば(10月5日以上かつ28日以下)
$horoscope = 'てんびん'; # てんびん座

}elsif($month==10 || ($month==11 && $day<=20)){ # 10月の29日以上~11月の20日以下ならば
$horoscope = 'さそり'; # さそり座

}elsif($month==11 || ($month==12 && $day<=14)){ # 11月の残り~12月の14日以下ならば
$horoscope = 'いて'; # いて座

}elsif($month==12){ # 12月の残り~12月の31日以下ならば
$horoscope = 'やぎ'; # やぎ座
}
}
return $horoscope;
}


#以下1950年の1月~12月の火星のデータとする

sub get_mars_data{
local($year,$month,$day)=@_;
local($horoscope)='undef';

if($year==1950){
if($month==1 && $day<=8){
# ~1月の8日以下ならば
$horoscope = 'やぎ';

}elsif($month==1){ # 1月の残りならば
$horoscope = 'みずがめ';

}elsif($month==2 && $day<=24){ # ~2月の24日以下ならば
$horoscope = 'うお';

}elsif($month==2 || ($month==3 && $day<=21)){ # 2月の残り~3月の21日以下ならば
$horoscope = 'おひつじ';

}elsif($month==3 || ($month==4 && $day<=15)){ # 3月の残り~4月の15日以下ならば
$horoscope = 'おうし';

}elsif($month==4 || ($month==5 && $day<=10)){ # 4月の残り~5月の10日以下ならば
$horoscope = 'ふたご';

}elsif($month==5 || ($month==6 && $day<=7)){ # 5月の残り~6月の7日以下ならば
$horoscope = 'かに';

}elsif($month==6 || ($month==7 && $day<=8)){ # 6月の残り~7月の8日以下ならば
$horoscope = 'しし';

}elsif($month<=10 || ($month==11 && $day<=9)){ # 10月の残り~11月の9日以下ならば$horoscope = 'おとめ';

}elsif($month==11 || ($month==12 && $day<=7)){ # 11月の残り~12月の7日以下ならば$horoscope = 'てんびん';

}elsif($month==12){ # 12月の残りならば
$horoscope = 'さそり';
}
}
return $horoscope;
}

# 入力から金星と火星の星座を求める。

$vinus = get_vinus_data($in{'year'}, $in{'month'}, $in{'day'});
$mars = get_mars_data($in{'year'}, $in{'month'}, $in{'day'});


print "Content-type:text/html\n\n";

print <<EOF;


<html>
<head>
<title>出力結果</title>
</head>

<body>

<font size="4">
金星は$vinus、火星は$mars
</font>

</body>
</html>

EOF

exit;

補足日時:2001/01/30 19:17
    • good
    • 0

すみません。


金星の関数定義と関数呼び出しでスペル違ってました。(恥ずかしい)
関数呼び出しのget_vinus_dataを
「vinus」→「venus」とすれば動くはずです。
変数名も恥ずかしいのでなおしておいてください。

やっぱり自分で実行してみないとだめっすね。
失礼しました。
    • good
    • 0
この回答へのお礼

きめ細かいご回答ありがとうございました。
表示することに成功いたしました!!
本当に感謝、感謝です。
また何かありましたら、今後ともよろしくお願い致します。m(_)m

お礼日時:2001/01/31 19:34

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