Arrays पर Iteration
In this page:
for value in "${array_name[@]}"; do
commands
done
for index in "${!array_name[@]}"; do
commands using "${array_name[index]}"
done
सिर्फ Values पर Iterate करना
Directly "${arr[@]}" पर loop करना सबसे simple option है जब आपको सिर्फ हर value चाहिए और array में इसकी position की care नहीं है। यह dense और sparse दोनों arrays के लिए identically काम करता है।
उदाहरण: Iterating Just the Values
#!/bin/bash
tasks=("write code" "run tests" "deploy")
for task in "${tasks[@]}"; do
echo "Task: $task"
done
Login to try C/C++/Java/PHP code in the editor
Sparse Arrays के लिए Indices पर Iterate करना
"${!arr[@]}" सिर्फ वे indices return करता है जिनका actually एक value assigned है, जो sparse arrays को correctly handle करता है जहां कुछ numeric positions skip हो गए थे। एक plain for (( i=0; i<len; i++ )) loop को missing indices skip करना नहीं पता होगा।
उदाहरण: Iterating Indices for Sparse Arrays
#!/bin/bash
declare -a sparse
sparse[0]="first"
sparse[3]="fourth"
for idx in "${!sparse[@]}"; do
echo "Index $idx has value: ${sparse[idx]}"
done
Login to try C/C++/Java/PHP code in the editor
Index और Value साथ में Iterate करना
"${!arr[@]}" पर loop करना और फिर loop body के अंदर ${arr[idx]} लुकअप करना हर pass पर आपको position और value दोनों देता है। यह pattern correctly काम करता है चाहे array dense हो या sparse।
उदाहरण: Iterating Index and Value Together
#!/bin/bash
players=(Alice Bob Carol)
for i in "${!players[@]}"; do
echo "Player #$i: ${players[i]}"
done
Login to try C/C++/Java/PHP code in the editor
एक Associative Array को Key से Iterate करना
Associative arrays के लिए, "${!arr[@]}" पर iterate करना आपको string keys देता है (क्योंकि कोई meaningful numeric position नहीं है), और आप हर value को ${arr[key]} से लुकअप करते हैं।
यह एक associative array के contents पर walk करने का इकलौता correct general-purpose तरीका है।
उदाहरण: Iterating an Associative Array by Key
#!/bin/bash
declare -A prices
prices["coffee"]=4
prices["tea"]=3
for item in "${!prices[@]}"; do
echo "$item costs \$${prices[$item]}"
done
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
!prefix का मतलब है 'मुझे indices दो', इसलिए${!arr[@]}और${arr[@]}similar दिखते हैं लेकिन पूरी तरह अलग चीज़ें return करते हैं (positions बनाम values)। - यह मान लेना कि
${!arr[@]}पर एकforloop हमेशा 0,1,2... जैसी एक contiguous range produce करेगा; अगर array sparse है, तो सिर्फ actually exist करने वाले indices return होते हैं। - एक sparse array पर एक C-style
for (( i=0; i<${#arr[@]}; i++ ))loop उपयोग करना और यह मान लेना कि हरiएक असली element से match करता है; उन indices में से कुछ exist नहीं कर सकते, silently empty values produce करते हुए।
for val in "${arr[@]}"index order में directly values पर iterate करता है।for idx in "${!arr[@]}"actually existing indices/keys पर iterate करता है, sparse arrays में gaps को correctly skip करते हुए।- जब भी आपको position और value साथ में चाहिए तो दोनों combine करें (indices पर loop करें, फिर
${arr[idx]}लुकअप करें)। - Associative arrays के लिए, सिर्फ
${!arr[@]}(keys) और key-से-lookup approach काम करता है -- iterate करने के लिए कोई meaningful 'numeric position' नहीं है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: