Associative Arrays का उपयोग
In this page:
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
#!/bin/bash
declare -A ages
ages["Ada"]=36
ages["Grace"]=85
ages["Linus"]=54
echo "Ada's age: ${ages["Ada"]}"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
declare -A capitals
capitals["France"]="Paris"
capitals["Japan"]="Tokyo"
capitals["Egypt"]="Cairo"
for country in "${!capitals[@]}"; do
echo "$country -> ${capitals[$country]}"
done
Login to try C/C++/Java/PHP code in the editor
यह 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
#!/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
Login to try C/C++/Java/PHP code in the editor
Entries Count करना और एक Key हटाना
${#arr[@]} एक associative array में key-value pairs की संख्या देता है, बिल्कुल जैसे यह indexed arrays के लिए देता है। unset arr[key] एक specific key-value pair को पूरी तरह हटा देता है।
उदाहरण: Counting Entries and Removing a Key
#!/bin/bash
declare -A stock
stock["apples"]=10
stock["bananas"]=5
echo "Entry count: ${#stock[@]}"
unset 'stock[bananas]'
echo "Entry count after removal: ${#stock[@]}"
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि associative array उपयोग करने से पहले
declare -Amandatory है; इसके बिना, Basharr[key]=valueको एक indexed array के रूप में treat करता है जहां एक non-numeric key coerce/misinterpret होती है, errors का कारण बनते हुए। - यह मान लेना कि associative arrays iterate होने पर insertion order preserve करते हैं; वे
${!arr[@]}या${arr[@]}के लिए किसी particular order की guarantee नहीं देते, indexed arrays के विपरीत। - ऐसी 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 भी रख सकती है)।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: