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で質問しましょう!
似たような質問が見つかりました
- JavaScript javascriptでif文と配列を組み合わせて運賃プログラムを完成させたいです。 1 2022/07/03 14:20
- Visual Basic(VBA) VBAの繰り返し処理について教えてください。 3 2022/08/02 13:21
- その他(プログラミング・Web制作) COBOL数値転記をCOPY句内での仕様 6 2022/06/15 18:48
- JavaScript セレクトボックスで配列を呼び出したい。 1 2022/07/08 20:14
- PHP PHP MySql ページング 2 2022/09/20 06:38
- Excel(エクセル) VBAで “:” を含むセルの特定 2 2023/05/11 16:30
- Java java 引数 戻り値のあるメソッド 3 2023/02/12 06:23
- JavaScript jsで、配列内の文章を改行する際どのようにすればいいですか。 3 2022/07/05 20:40
- C言語・C++・C# c言語 プログラムのエラー 1 2023/02/11 20:31
- JavaScript HTMLでJavaScriptを使ってパスワードの強化判定のプログラムを作成しています。 一通り作っ 2 2022/10/19 01:41
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
前日の日付取得するには?
-
Perlでsprintf("%02d",$month)...
-
1桁の日付に0を付ける
-
Application.ScreenUpdating = ...
-
JSONで文字列が長い時
-
formで特定のinputを送信しない...
-
<SELECT>タグの折り返し
-
16進の10進変換について
-
実行時エラー 3020の対策
-
文字の横にプルダウンを表示さ...
-
デ-タテ-ブルの内容での更新...
-
セレクトメニューで2つの項目...
-
数値英単語変換
-
セレクトボックスのselected属...
-
複数列を持ったリストボックス...
-
selectタグ内の特定のoptionの...
-
【メモリ不足で落ちる(python)】
-
STLのlistで重複するものだけを...
-
【至急!!!】python言語で本を見...
-
パイソンのクラスについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Perlでsprintf("%02d",$month)...
-
1桁の日付に0を付ける
-
日齢計算プログラム
-
Perlの日付の比較に関して
-
当日の日付をプルダウンメニュ...
-
Cookieの有効期限について
-
CSVファイルに出力
-
日付の差分の求め方(日、分)
-
うるう年の出し方
-
Schemeのプログラミング うる...
-
Perlで60進数を作りたい
-
PHPのstrtotimeのようなものあ...
-
2つの日付・時間の差分を求め...
-
a shower.
-
日付範囲をの連番と月末を取得
-
体重が3kg増える/減る の英語...
-
C言語でのPICプログラミング
-
前日の日付取得するには?
-
ソースの詳細を教えてください。
-
PerlからMysqlに接続してデータ...
おすすめ情報