"ForEach"의 두 판 사이의 차이

잔글 (Jmnote님이 Foreach 문서를 ForEach 문서로 이동했습니다)
 
(사용자 3명의 중간 판 32개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{다른뜻|jQuery .each()}}
[[category:Array]]
[[category:Array]]
[[category:Dictionary]]
[[category:Dictionary]]
5번째 줄: 6번째 줄:
;foreach ... as
;foreach ... as
;foreach ... in
;foreach ... in
;for ... in
;each
;each


==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
{{참고|Bash for}}
<syntaxhighlight lang='bash'>
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for VALUE in "${ARR[@]}"
for VALUE in "${ARR[@]}"; do
do
echo "[$VALUE]"
echo "[$VALUE]"
done
done
19번째 줄: 21번째 줄:
# [Mike Barnes]
# [Mike Barnes]
# [Kevin Patterson]
# [Kevin Patterson]
</source>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
echo $i: ${ARR[$i]}
done
# 0: John Smith
# 1: Jane Doe
# 2: Mike Barnes
# 3: Kevin Patterson
</syntaxhighlight>
<syntaxhighlight lang='bash'>
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
echo $((i+1)): ${ARR[$i]}
done
# 1: John Smith
# 2: Jane Doe
# 3: Mike Barnes
# 4: Kevin Patterson
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang='csharp'>
<syntaxhighlight lang='csharp'>
// http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx
// http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
30번째 줄: 52번째 줄:
   System.Console.WriteLine(i);
   System.Console.WriteLine(i);
}
}
</source>
</syntaxhighlight>
 
==Go==
[[분류: Go]]
{{참고|Go foreach}}
<syntaxhighlight lang='go'>
package main
 
import "fmt"
 
func main() {
fruits := []string{"Apple", "Banana", "Orange"}
for _, fruit := range fruits {
fmt.Println(fruit)
}
}
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang='java'>
{{참고|Java Foreach}}
String[] strArr = new String[] { "A", "B", "C", "D" };
<syntaxhighlight lang='java'>
String[] strArr = { "John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson" };
for (String str : strArr){
for (String str : strArr){
  System.out.println(str);
System.out.println(str);
}
}
</source>
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
</syntaxhighlight>


==JavaScript==
==JavaScript==
*i is not value but key!
*i is not value but key!
[[category: Javascript]]
[[category: JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
for(var i in arr) {
53번째 줄: 96번째 줄:
// Mike Barnes
// Mike Barnes
// Kevin Patterson
// Kevin Patterson
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
for(var i in arr) {
63번째 줄: 106번째 줄:
// 2: Mike Barnes
// 2: Mike Barnes
// 3: Kevin Patterson
// 3: Kevin Patterson
</source>
</syntaxhighlight>


==jQuery==
==jQuery==
[[category:jQuery]]
[[category:jQuery]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function() {
$.each(arr, function() {
76번째 줄: 119번째 줄:
// Mike Barnes
// Mike Barnes
// Kevin Patterson
// Kevin Patterson
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function(i) {
$.each(arr, function(i, val) {
   console.log(i+': '+this);
   console.log(i+': '+val);
});
});
// 0: John Smith
// 0: John Smith
86번째 줄: 129번째 줄:
// 2: Mike Barnes
// 2: Mike Barnes
// 3: Kevin Patterson
// 3: Kevin Patterson
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
NSArray *strArr = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D"];
NSArray *strArr = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D"];
for(NSString *str in strArr) {
for(NSString *str in strArr) {
   NSLog(@"%@\n", str);
   NSLog(@"%@\n", str);
}
}
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl'>
@strArr = ('A', 'B', 'C', 'D');
@strArr = ('A', 'B', 'C', 'D');
foreach $str (@strArr) {
foreach $str (@strArr) {
120번째 줄: 163번째 줄:
}
}
# A B C D
# A B C D
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$strArr = array('A', 'B', 'C', 'D');
$strArr = array('A', 'B', 'C', 'D');
foreach ($strArr as $str) {
foreach ($strArr as $str) {
130번째 줄: 173번째 줄:
}
}
// A B C D
// A B C D
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
foreach (range(1,5) as $i) {
foreach (range(1,5) as $i) {
   echo "$i ";
   echo "$i ";
}
}
// 1 2 3 4 5
// 1 2 3 4 5
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');
$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');
foreach($fruits as $key => $value) {
foreach($fruits as $key => $value) {
145번째 줄: 188번째 줄:
// b_banana
// b_banana
// c_cranberry
// c_cranberry
</source>
</syntaxhighlight>
 
==PowerShell==
[[분류: PowerShell]]
<syntaxhighlight lang='PowerShell'>
$arr = @(1,"Hello",3.5)
foreach ($v in $arr) {$v}
# 1
# Hello
# 3.5
</syntaxhighlight>
<syntaxhighlight lang='PowerShell'>
$arr = @(1,"Hello",3.5)
$arr
# 1
# Hello
# 3.5
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='python'>
{{소스헤더|list}}
<syntaxhighlight lang='python'>
fruits = ['Banana', 'Orange', 'Apple', 'Mango']
fruits = ['Banana', 'Orange', 'Apple', 'Mango']
for fruit in fruits:
for fruit in fruits:
print fruit
print( fruit )
# Banana
# Banana
# Orange
# Orange
# Apple
# Apple
# Mango
# Mango
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python'>
for fruit in ('Banana', 'Orange', 'Apple', 'Mango'):
print( fruit )
</syntaxhighlight>
<syntaxhighlight lang='Python'>
for ch in 'ABC':
print( ch )
</syntaxhighlight>
{{소스헤더|dictionary}}
<syntaxhighlight lang='python'>
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for key in fruits:
for key in fruits:
165번째 줄: 235번째 줄:
# c_cranberry
# c_cranberry
# b_banana
# b_banana
</source>
</syntaxhighlight>
<syntaxhighlight lang='python'>
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for k, v in fruits.items():
print k + '_' + v
# a_apple
# c_cranberry
# b_banana
</syntaxhighlight>
 
==R==
[[분류: R]]
{{참고|R for}}
<syntaxhighlight lang='r'>
v <- c("John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson")
for( s in v ) {
    print( s )
}
## [1] "John Smith"
## [1] "Jane Doe"
## [1] "Mike Barnes"
## [1] "Kevin Patterson"
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='ruby'>
{{소스헤더|array}}
strArr = ['A', 'B', 'C', 'D']
<syntaxhighlight lang='ruby'>
strArr = ['A', 'B', 'C']
strArr.each do |str|
    puts str
end
# A
# B
# C
</syntaxhighlight>
<syntaxhighlight lang='ruby'>
strArr = ['A', 'B', 'C']
strArr.each { |str| puts str }
# A
# B
# C
</syntaxhighlight>
<syntaxhighlight lang='ruby'>
strArr = ['A', 'B', 'C']
for str in strArr
for str in strArr
   puts str
   puts str
end
end
# A
# B
# C
</syntaxhighlight>


strArr.each { |str| puts str }
{{소스헤더|dictionary}}
</source>
<syntaxhighlight lang='ruby'>
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each { |x, y| puts "#{x}: #{y}" }
# ID: 102
# Name: YONEZAWA Akinori
</syntaxhighlight>
<syntaxhighlight lang='ruby'>
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each do |x, y|
    puts "#{x}: #{y}"
end
# ID: 102
# Name: YONEZAWA Akinori
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang='vb'>
<syntaxhighlight lang='vb'>
For Each number As Integer In New Long() {45, 3, 987}
For Each number As Integer In New Long() {45, 3, 987}
   Console.Write(number & " ")
   Console.Write(number & " ")
Next
Next
' Output: 45 3 987
' Output: 45 3 987
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[for]]
* [[for]]
*[[while]]
* [[items]]
*[[continue]]
* [[while]]
*[[print_r]]
* [[continue]]
*[[Variable-length argument lists]]
* [[print_r]]
*[[array_push]]
* [[Variable-length argument lists]]
*[[제어문]]
* [[array]]
*[[C샵 키워드|C# 키워드]]
* [[dictionary]]
* [[array_push]]
* [[함수 count()]]
* [[함수 map()]]
* [[제어문]]
* [[C샵 키워드|C# 키워드]]


==참고 자료==
==참고==
*http://eqcode.com/wiki/Foreach
*http://eqcode.com/wiki/Foreach

2022년 3월 27일 (일) 17:47 기준 최신판

  다른 뜻에 대해서는 jQuery .each() 문서를 참조하십시오.

extended for
foreach ... as
foreach ... in
for ... in
each

1 Bash[ | ]

ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for VALUE in "${ARR[@]}"; do
	echo "[$VALUE]"
done
# [Jonh Smith]
# [Jane Doe]
# [Mike Barnes]
# [Kevin Patterson]
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
	echo $i: ${ARR[$i]}
done
# 0: John Smith
# 1: Jane Doe
# 2: Mike Barnes
# 3: Kevin Patterson
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
	echo $((i+1)): ${ARR[$i]}
done
# 1: John Smith
# 2: Jane Doe
# 3: Mike Barnes
# 4: Kevin Patterson

2 C#[ | ]

// http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in fibarray)
{
  System.Console.WriteLine(i);
}

3 Go[ | ]

package main

import "fmt"

func main() {
	fruits := []string{"Apple", "Banana", "Orange"}
	for _, fruit := range fruits {
		fmt.Println(fruit)
	}
}

4 Java[ | ]

String[] strArr = { "John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson" };
for (String str : strArr){
	System.out.println(str);
}
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson

5 JavaScript[ | ]

  • i is not value but key!
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
  console.log(arr[i]);
}
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
  console.log(i+": "+arr[i]);
}
// 0: John Smith
// 1: Jane Doe
// 2: Mike Barnes
// 3: Kevin Patterson

6 jQuery[ | ]

var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function() {
  console.log(''+this);
});
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function(i, val) {
  console.log(i+': '+val);
});
// 0: John Smith
// 1: Jane Doe
// 2: Mike Barnes
// 3: Kevin Patterson

7 Objective-C[ | ]

NSArray *strArr = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D"];
for(NSString *str in strArr) {
  NSLog(@"%@\n", str);
}

8 Perl[ | ]

@strArr = ('A', 'B', 'C', 'D');
foreach $str (@strArr) {
  print "$str ";
}
# A B C D

foreach (@strArr) {
  print "$_ ";
}
# A B C D

for $str (@strArr) {
  print "$str ";
}
# A B C D

for (@strArr) {
  print "$_ ";
}
# A B C D

9 PHP[ | ]

$strArr = array('A', 'B', 'C', 'D');
foreach ($strArr as $str) {
  echo $str.' ';
}
// A B C D
foreach (range(1,5) as $i) {
  echo "$i ";
}
// 1 2 3 4 5
$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');
foreach($fruits as $key => $value) {
	echo $key."_".$value."\n";
}
// a_apple
// b_banana
// c_cranberry

10 PowerShell[ | ]

$arr = @(1,"Hello",3.5)
foreach ($v in $arr) {$v}
# 1
# Hello
# 3.5
$arr = @(1,"Hello",3.5)
$arr
# 1
# Hello
# 3.5

11 Python[ | ]

list
fruits = ['Banana', 'Orange', 'Apple', 'Mango']
for fruit in fruits:
	print( fruit )
# Banana
# Orange
# Apple
# Mango
for fruit in ('Banana', 'Orange', 'Apple', 'Mango'):
	print( fruit )
for ch in 'ABC':
	print( ch )
dictionary
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for key in fruits:
	print key + '_' + fruits[key]
# a_apple
# c_cranberry
# b_banana
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for k, v in fruits.items():
	print k + '_' + v
# a_apple
# c_cranberry
# b_banana

12 R[ | ]

v <- c("John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson")
for( s in v ) {
    print( s )
}
## [1] "John Smith"
## [1] "Jane Doe"
## [1] "Mike Barnes"
## [1] "Kevin Patterson"

13 Ruby[ | ]

array
strArr = ['A', 'B', 'C']
strArr.each do |str|
    puts str
end
# A
# B
# C
strArr = ['A', 'B', 'C']
strArr.each { |str| puts str }
# A
# B
# C
strArr = ['A', 'B', 'C']
for str in strArr
  puts str
end
# A
# B
# C
dictionary
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each { |x, y| puts "#{x}: #{y}" }
# ID: 102
# Name: YONEZAWA Akinori
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each do |x, y|
    puts "#{x}: #{y}"
end
# ID: 102
# Name: YONEZAWA Akinori

14 VB[ | ]

For Each number As Integer In New Long() {45, 3, 987}
  Console.Write(number & " ")
Next
' Output: 45 3 987

15 같이 보기[ | ]

16 참고[ | ]