0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

それぞれのファイルの合計値を出してみた

Posted at

はじめに

以下のそれぞれ数値が記載されたファイルがカレントディレクトリにあります。

data.txt
10
100
10
data1.txt
20
30

ファイルの中の数値の合計を出すpowershellを作成しました。

data.ps1
cat ./data.txt |foreach { $sum += [int]$_ }
cat ./data1.txt |foreach { $sum += [int]$_ }
$sum

実行すると以下の通り合計値が出ます。

170

配列にしてファイルの中身を簡潔にする

2つのファイルなのでそれほど冗長な感じはしませんが数が多くなってくると何度も記載することになり、面倒なので配列にして簡潔に書きます。

data.ps1
$data = @('data.txt','data1.txt')
cat $data |foreach { $sum += [int]$_ }
$sum

配列の書き方は「変数 = @(要素1、要素2)」という感じですね。
それぞれのファイル(data.txt、data1.txt)を配列にします。

実行結果は以下の通りです。

170

配列を書いて慣れていきたかったので書きました。
数が多くなった時便利ですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?