No.1
- 回答日時:
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構文で出来ますでしょうか?
これでなんとなく伝わりましたでしょうか?
まだ分かりませんでしたら、お知らせ下さい。
よろしくお願いいたします。
No.2
- 回答日時:
質問が出されたときから、何とか理解しようとしましたが、
どうも理解できなかったので、他の人が回答するのを待っていました。
で以下の回答と回答の補足を読んだのですが、
やっぱりわかりません(笑)
現在の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日ならば「金星はみずがめ、火星はやぎ」
というように両方とも表示できるようにしたいのですが・・
どうぞよろしくお願い致します。
No.3
- 回答日時:
データと月の関係の法則が決まっているならば、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文がどのように配列に置き換えることが
できるのか具体的にお見せ頂けますでしょうか?
大変お手数をおかけしますが、よろしくお願い致します。
No.4ベストアンサー
- 回答日時:
あまり単純な計算式にはできないようですね。
配列で……ってのは忘れてください。
んで、
> #例えば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;
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
1桁の日付に0を付ける
-
日付の差分の求め方(日、分)
-
Perlの日付の比較に関して
-
JSONで文字列が長い時
-
Application.ScreenUpdating = ...
-
VBAでPDFのコピーとリネームを...
-
メモリをアドレスを直接指定し...
-
「*:*」って何を意味するのでし...
-
実行中の変数の中身をイミディ...
-
UWSC SLCTBOXもしくはSELECTに...
-
C++ APIについて エラーコード...
-
[python] 文字列を変数名として...
-
Net::Telnetでタイムアウト時間...
-
UWSCのSELECT文の記述方法
-
メ-ルにno problem@の前にname...
-
VBAのNext forでエラーが出て困...
-
Perl:CGI.pm:入力ボックスのク...
-
エクセルマクロでフォルダのコ...
-
perl内のhtml文を、ループさせ...
-
VB6で、長い時間かかる処理...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
おすすめ情報