"Replace recursive()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
4번째 줄: 4번째 줄:
==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$computer1 = array(
$computer1 = array(
'owner' => 'John',
'owner' => 'John',
57번째 줄: 57번째 줄:
#  
#  
# )
# )
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='ruby'>
<syntaxhighlight lang='ruby'>
def replace_recursive(base, replacements)
def replace_recursive(base, replacements)
def replace_recursive_real(base, replacements)
def replace_recursive_real(base, replacements)
92번째 줄: 92번째 줄:
puts replace_recursive(computer2, computer1).inspect
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
</source>
</syntaxhighlight>
<source lang='ruby'>
<syntaxhighlight lang='ruby'>
def replace_recursive(base, replacements)
def replace_recursive(base, replacements)
def replace_recursive_route(base, replacements)
def replace_recursive_route(base, replacements)
130번째 줄: 130번째 줄:
puts replace_recursive(computer2, computer1).inspect
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[array_replace_recursive()]]
*[[array_replace_recursive()]]

2020년 11월 2일 (월) 02:35 기준 최신판

associative array replace recursive
replace_recursive()

1 PHP[ | ]

$computer1 = array(
	'owner' => 'John',
	'softwares' => array( 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ),
	'peripherals' => array( 'keyboard' => 'Samsung' ),
);
$computer2 = array(
	'owner' => 'Jane',
	'softwares' => array( 'Linux', 'Apache', 'MySQL', 'PHP' ),
	'peripherals' => array( 'keyboard' => 'LG', 'mouse' => 'LG' ),
);

print_r( array_replace_recursive($computer1, $computer2) );
# Array
# (
#     [owner] => Jane
#     [softwares] => Array
#         (
#             [0] => Linux
#             [1] => Apache
#             [2] => MySQL
#             [3] => PHP
#             [4] => Apache
#         )
# 
#     [peripherals] => Array
#         (
#             [keyboard] => LG
#             [mouse] => LG
#         )
# 
# )

print_r( array_replace_recursive($computer2, $computer1) );
# Array
# (
#     [owner] => John
#     [softwares] => Array
#         (
#             [0] => Windows
#             [1] => Office
#             [2] => Photoshop
#             [3] => Python
#             [4] => Apache
#         )
# 
#     [peripherals] => Array
#         (
#             [keyboard] => Samsung
#             [mouser] => LG
#         )
# 
# )

2 Ruby[ | ]

def replace_recursive(base, replacements)
	def replace_recursive_real(base, replacements)
		if ( replacements.kind_of?(Array) )
			replacements.each_with_index do | v, i | base[i] = replace_recursive_real(base[i], v) end
			return base
		elsif ( replacements.kind_of?(Hash) )
			replacements.each do | k, v | base[k] = replace_recursive_real(base[k], v) end
			return base
		end
		replacements
	end
	replace_recursive_real(Marshal.load(Marshal.dump(base)), replacements)
end

computer1 = {
	'owner' => 'John',
	'softwares' => [ 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ],
	'peripherals' => { 'keyboard' => 'Samsung' },
	'a' => 'hello'
}
computer2 = {
	'owner' => 'Jane',
	'softwares' => [ 'Linux', 'Apache', 'MySQL', 'PHP' ],
	'peripherals' => {'keyboard' => 'LG', 'mouse' => 'LG' },
	'b' => 'world'
}
puts replace_recursive(computer1, computer2).inspect
# {"owner"=>"Jane", "softwares"=>["Linux", "Apache", "MySQL", "PHP", "Apache"], "peripherals"=>{"keyboard"=>"LG", "mouse"=>"LG"}, "a"=>"hello", "b"=>"world"}
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}
def replace_recursive(base, replacements)
	def replace_recursive_route(base, replacements)
		if ( replacements.kind_of?(Array) ) then replace_recursive_array(base, replacements)
		elsif ( replacements.kind_of?(Hash) ) then replace_recursive_hash(base, replacements)
		else replacements end
	end

	def replace_recursive_array(base, replacements)
		replacements.each_with_index do | v, i | base[i] = replace_recursive_route(base[i], v) end
		base
	end
	
	def replace_recursive_hash(base, replacements)
		replacements.each do | k, v | base[k] = replace_recursive_route(base[k], v) end
		base
	end

	replace_recursive_route(Marshal.load(Marshal.dump(base)), replacements)
end

computer1 = {
	'owner' => 'John',
	'softwares' => [ 'Windows', 'Office', 'Photoshop', 'Python', 'Apache' ],
	'peripherals' => { 'keyboard' => 'Samsung' },
	'a' => 'hello'
}
computer2 = {
	'owner' => 'Jane',
	'softwares' => [ 'Linux', 'Apache', 'MySQL', 'PHP' ],
	'peripherals' => {'keyboard' => 'LG', 'mouse' => 'LG' },
	'b' => 'world'
}
puts replace_recursive(computer1, computer2).inspect
# {"owner"=>"Jane", "softwares"=>["Linux", "Apache", "MySQL", "PHP", "Apache"], "peripherals"=>{"keyboard"=>"LG", "mouse"=>"LG"}, "a"=>"hello", "b"=>"world"}
puts replace_recursive(computer2, computer1).inspect
# {"owner"=>"John", "softwares"=>["Windows", "Office", "Photoshop", "Python", "Apache"], "peripherals"=>{"keyboard"=>"Samsung", "mouse"=>"LG"}, "b"=>"world", "a"=>"hello"}

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}