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

Associative Arrays का उपयोग

एक associative array numeric positions के बजाय text keys के under values store करता है, names से values के एक lookup table जैसे काम करते हुए।
Syntax
bash
declare -A array_name
array_name[key]=value
echo "${array_name[key]}"
for key in "${!array_name[@]}"; do
  echo "$key: ${array_name[$key]}"
done

एक Associative Array Declare करना

Associative arrays को कोई key-value pairs assign करने से पहले एक explicit declare -A arrname चाहिए, indexed arrays के विपरीत जो implicitly बनाए जा सकते हैं। एक बार declare होने पर, values brackets के अंदर string keys उपयोग करके set की जाती हैं, arr["key"]=value।

उदाहरण: Declaring an Associative Array

bash
#!/bin/bash
declare -A ages
ages["Ada"]=36
ages["Grace"]=85
ages["Linus"]=54
echo "Ada's age: ${ages["Ada"]}"

Keys और Values पर Iterate करना

${!arr[@]} associative array में currently मौजूद सभी keys में expand होता है, और ${arr[key]} एक specific key के लिए value लुकअप करता है, तो keys पर एक loop को lookup के साथ combine करने से हर key-value pair मिल जाता है।

Iteration का order insertion order से match होने की guarantee नहीं है।

उदाहरण: Iterating Over Keys and Values

bash
#!/bin/bash
declare -A capitals
capitals["France"]="Paris"
capitals["Japan"]="Tokyo"
capitals["Egypt"]="Cairo"
for country in "${!capitals[@]}"; do
    echo "$country -> ${capitals[$country]}"
done

यह Check करना कि एक Key Exist करती है या नहीं

[[ -v arr[key] ]] test करता है कि एक दिया गया key associative array में exist करता है या नहीं, चाहे इसका value empty हो या नहीं, जो lookup किए गए value के non-empty string होने की checking से अलग है।

यह distinction मायने रखता है क्योंकि एक key legitimately एक empty string पर set हो सकता है।

उदाहरण: Checking If a Key Exists

bash
#!/bin/bash
declare -A settings
settings["debug"]=""
if [[ -v settings["debug"] ]]; then
    echo "'debug' key exists (even though its value is empty)"
fi
if [[ ! -v settings["missing"] ]]; then
    echo "'missing' key does not exist"
fi

Entries Count करना और एक Key हटाना

${#arr[@]} एक associative array में key-value pairs की संख्या देता है, बिल्कुल जैसे यह indexed arrays के लिए देता है। unset arr[key] एक specific key-value pair को पूरी तरह हटा देता है।

उदाहरण: Counting Entries and Removing a Key

bash
#!/bin/bash
declare -A stock
stock["apples"]=10
stock["bananas"]=5
echo "Entry count: ${#stock[@]}"
unset 'stock[bananas]'
echo "Entry count after removal: ${#stock[@]}"
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. यह भूल जाना कि associative array उपयोग करने से पहले declare -A mandatory है; इसके बिना, Bash arr[key]=value को एक indexed array के रूप में treat करता है जहां एक non-numeric key coerce/misinterpret होती है, errors का कारण बनते हुए।
  2. यह मान लेना कि associative arrays iterate होने पर insertion order preserve करते हैं; वे ${!arr[@]} या ${arr[@]} के लिए किसी particular order की guarantee नहीं देते, indexed arrays के विपरीत।
  3. ऐसी unquoted keys उपयोग करना जिन्हें glob patterns समझ लिया जाए या जिनमें spaces हों, lookups को fail होने या unexpectedly behave करने का कारण बनते हुए।
चैप्टर सारांश
  • पहले explicitly declare -A arrname से declare किया जाना चाहिए -- कोई implicit associative array creation नहीं है।
  • Keys arbitrary strings हैं, जैसे ages["Ada"]=36, sequential integers के बजाय।
  • ${!arr[@]} सभी keys देता है; ${arr[@]} सभी values देता है (order की guarantee नहीं है)।
  • यह check करना कि एक key exist करती है या नहीं [ -v arr[key] ] (या [[ -v arr[key] ]]) या ${arr[key]+_} से किया जाता है, यह check करके नहीं कि value non-empty है (एक key legitimately एक empty string भी रख सकती है)।

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.