← Back to Bash Course | Chapter 6: Arrays | Lesson 7 of 10

Arrays पर Iteration

एक array पर loop करने के कुछ अलग तरीके हैं इस आधार पर कि आपको सिर्फ values चाहिए, सिर्फ positions, या दोनों एक साथ।
Syntax
bash
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

bash
#!/bin/bash
tasks=("write code" "run tests" "deploy")
for task in "${tasks[@]}"; do
    echo "Task: $task"
done

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

bash
#!/bin/bash
declare -a sparse
sparse[0]="first"
sparse[3]="fourth"
for idx in "${!sparse[@]}"; do
    echo "Index $idx has value: ${sparse[idx]}"
done

Index और Value साथ में Iterate करना

"${!arr[@]}" पर loop करना और फिर loop body के अंदर ${arr[idx]} लुकअप करना हर pass पर आपको position और value दोनों देता है। यह pattern correctly काम करता है चाहे array dense हो या sparse।

उदाहरण: Iterating Index and Value Together

bash
#!/bin/bash
players=(Alice Bob Carol)
for i in "${!players[@]}"; do
    echo "Player #$i: ${players[i]}"
done

एक 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

bash
#!/bin/bash
declare -A prices
prices["coffee"]=4
prices["tea"]=3
for item in "${!prices[@]}"; do
    echo "$item costs \$${prices[$item]}"
done
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि ! prefix का मतलब है 'मुझे indices दो', इसलिए ${!arr[@]} और ${arr[@]} similar दिखते हैं लेकिन पूरी तरह अलग चीज़ें return करते हैं (positions बनाम values)।
  2. यह मान लेना कि ${!arr[@]} पर एक for loop हमेशा 0,1,2... जैसी एक contiguous range produce करेगा; अगर array sparse है, तो सिर्फ actually exist करने वाले indices return होते हैं।
  3. एक 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' नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.