Difference between revisions of "Basic Pascal Tutorial/Chapter 5/1-dimensional arrays/ja"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{1-dimensional arrays/ja}} 5C - 1次元配列 (著者: Tao Yue, 状態: 原文のまま変更なし) Suppose you wanted to read in 5000 integers and do something with the...")
 
m (bypass language bar/categorization template redirect [cf. discussion])
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{1-dimensional arrays/ja}}
+
{{Basic Pascal Tutorial/Chapter 5/1-dimensional arrays}}
  
 
5C - 1次元配列 (著者: Tao Yue, 状態: 原文のまま変更なし)
 
5C - 1次元配列 (著者: Tao Yue, 状態: 原文のまま変更なし)
  
Suppose you wanted to read in 5000 integers and do something with them. How would you store the integers?
+
5000個の整数を読み込み、何か処理をしたいと考えてみよう。それらの整数をどう保存しておけばよいだろうか?
  
You could use 5000 variables, lapsing into:
+
次のように5000個の変数を使うかもしれない。
 
  aa, ab, ac, ad, ... aaa, aab, ... aba, ...
 
  aa, ab, ac, ad, ... aaa, aab, ... aba, ...
  
But this would grow tedious (after declaring those variables, you have to read values into each of those variables).
+
しかし、これは煩雑になる(これらの変数を宣言した後で、変数それぞれに値を読み込まなくてはならない)。
  
An array contains several storage spaces, all the same type. You refer to each storage space with the array name and with a subscript. The type definition is:
+
配列は同じタイプの保存スペースを含んでいる。それぞれの保存スペースは添え字付きの配列名で参照できる。定義は次のようになる。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
   typename = array [enumerated_type] of another_data_type;
+
   typename = array [列挙型] of データ型;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The data type can be anything, even another array. Any enumerated type will do. You can specify the enumerated type inside the brackets, or use a predefined enumerated type. In other words,
+
データ型は何でもよく、別な配列でもよい。どんな列挙型も問題ない。カッコ内に列挙型、あるいは事前に定義した列挙型を指定できる。例えば、次のようになる。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   enum_type = 1..50;
 
   enum_type = 1..50;
Line 23: Line 23:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
is equivalent to
+
上の例は以下の例と同じである。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   arraytype = array [1..50] of integer;
 
   arraytype = array [1..50] of integer;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Aside: This is how strings are actually managed internally — as arrays. Back before modern Pascal compilers added native support for strings, programmer had to handle it themselves, by declaring:
+
余談: これは実際、文字列が内部的に処理されているやり方、すなわち配列として処理されている方法である。現代の Pascal コンパイラが文字列のネイティブなサポートを加える前は、プログラマーは以下のように宣言し、さらに文字列の最後を示すある種の終端文字を用いて自ら文字列を処理しなくてはならなかった。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   String = packed array [0..255] of char;
 
   String = packed array [0..255] of char;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
and using some kind of terminating character to signify the end of the string. Most of the time it's the null-character (ordinal number 0, or ord(0)). The packed specifier means that the array will be squeezed to take up the smallest amount of memory.
+
大抵の場合、終端文字はヌル・キャラクター (序数 0, あるいは ord(0))であった。packed という指定子はその配列が、最小のメモリーを占めるよう圧縮されることを意味している。
  
Arrays of characters representing strings are often referred to as buffers, and errors in handling them in the C or C++ programming languages may lead to buffer overruns. A buffer overrun occurs when you try to put, say, a 200-character string into a 150-length array. If memory beyond the buffer is overwritten, and if that memory originally contained executable code, then the attacker has just managed to inject arbitrary code into your system. This is what caused the famous Slammer worm that ran rampant on the Internet for several days. Try it in Pascal and see what happens.
+
文字列を表すキャラクターの配列はしばしばバッファと呼ばれ、C や C++ のプログラミング言語でそれらを扱う際のエラーはバッファー・オーバーランにつながることもある。バッファ・オーバーランは、たとえば200キャラクタの文字を150の長さの配列に入れようとするときに生じる。もし、バッファを超えたメモリが上書きされ、もし、そのメモリにもとは実行コードが含まれていたなら、攻撃者は任意のコードをシステムに挿入しようとするかもしれない。これはインターネット上で数日間猛威を振るった有名なスラマー・ウォーム(Slammer worm)の原因にもなった。Pascal でそれをやって、何が起きるか試してみよう。
  
Arrays are useful if you want to store large quantities of data for later use in the program. They work especially well with for loops, because the index can be used as the subscript. To read in 50 numbers, assuming the following definitions:
+
配列はプログラムで後から利用するために大きなデータを保存しておきたい場合には有用である。特にループではうまく働く。なぜならインデックスが添え字として利用できるからである。50個の数を読むためには、以下の定義のようになる。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   arraytype = array[1..50] of integer;
 
   arraytype = array[1..50] of integer;
Line 48: Line 48:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   
 
   
use:
+
以下のようにすればよい。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
for count := 1 to 50 do
 
for count := 1 to 50 do
 
   read (myarray[count]);
 
   read (myarray[count]);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Brackets <tt>[ ]</tt> enclose the subscript when referring to arrays.
+
四角カッコの <tt>[ ]</tt> は配列の場合には添え字を囲む。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
myarray[5] := 6;
 
myarray[5] := 6;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Subranges/ja|previous]]   
+
|[[Basic Pascal Tutorial/Chapter 5/Subranges/ja|previous]]   
|[[Contents/ja|contents]]  
+
|[[Basic Pascal Tutorial/Contents/ja|contents]]  
|[[Multidimensional_arrays/ja|next]]
+
|[[Basic Pascal Tutorial/Chapter 5/Multidimensional arrays/ja|next]]
 
|}
 
|}

Latest revision as of 16:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

5C - 1次元配列 (著者: Tao Yue, 状態: 原文のまま変更なし)

5000個の整数を読み込み、何か処理をしたいと考えてみよう。それらの整数をどう保存しておけばよいだろうか?

次のように5000個の変数を使うかもしれない。

aa, ab, ac, ad, ... aaa, aab, ... aba, ...

しかし、これは煩雑になる(これらの変数を宣言した後で、変数それぞれに値を読み込まなくてはならない)。

配列は同じタイプの保存スペースを含んでいる。それぞれの保存スペースは添え字付きの配列名で参照できる。定義は次のようになる。

type
  typename = array [列挙型] of データ型;

データ型は何でもよく、別な配列でもよい。どんな列挙型も問題ない。カッコ内に列挙型、あるいは事前に定義した列挙型を指定できる。例えば、次のようになる。

type
  enum_type = 1..50;
  arraytype = array [enum_type] of integer;

上の例は以下の例と同じである。

type
  arraytype = array [1..50] of integer;

余談: これは実際、文字列が内部的に処理されているやり方、すなわち配列として処理されている方法である。現代の Pascal コンパイラが文字列のネイティブなサポートを加える前は、プログラマーは以下のように宣言し、さらに文字列の最後を示すある種の終端文字を用いて自ら文字列を処理しなくてはならなかった。

type
  String = packed array [0..255] of char;

大抵の場合、終端文字はヌル・キャラクター (序数 0, あるいは ord(0))であった。packed という指定子はその配列が、最小のメモリーを占めるよう圧縮されることを意味している。

文字列を表すキャラクターの配列はしばしばバッファと呼ばれ、C や C++ のプログラミング言語でそれらを扱う際のエラーはバッファー・オーバーランにつながることもある。バッファ・オーバーランは、たとえば200キャラクタの文字を150の長さの配列に入れようとするときに生じる。もし、バッファを超えたメモリが上書きされ、もし、そのメモリにもとは実行コードが含まれていたなら、攻撃者は任意のコードをシステムに挿入しようとするかもしれない。これはインターネット上で数日間猛威を振るった有名なスラマー・ウォーム(Slammer worm)の原因にもなった。Pascal でそれをやって、何が起きるか試してみよう。

配列はプログラムで後から利用するために大きなデータを保存しておきたい場合には有用である。特にループではうまく働く。なぜならインデックスが添え字として利用できるからである。50個の数を読むためには、以下の定義のようになる。

type
  arraytype = array[1..50] of integer;
  
var
  myarray : arraytype;

以下のようにすればよい。

for count := 1 to 50 do
  read (myarray[count]);

四角カッコの [ ] は配列の場合には添え字を囲む。

myarray[5] := 6;
previous contents next