Array Slicing का उपयोग
In this page:
${array_name[@]:offset:length}
${array_name[@]:offset}
Offset और Length के साथ Basic Slicing
${arr[@]:offset:length} elements की एक sub-list निकालता है जो index offset से शुरू होती है, वहां से length elements तक लेते हुए। यह manually loop करने और हाथ से एक नया array बनाने से बचाता है।
उदाहरण: Basic Slicing with Offset and Length
#!/bin/bash
letters=(a b c d e f)
slice=("${letters[@]:1:3}")
echo "Slice: ${slice[@]}"
Login to try C/C++/Java/PHP code in the editor
Length Omit करना
Length argument छोड़ देना, ${arr[@]:offset}, offset से लेकर array के end तक हर element लेता है। यह 'इस point के बाद सब कुछ' कहने का एक convenient तरीका है।
उदाहरण: Omitting the Length
#!/bin/bash
numbers=(10 20 30 40 50)
rest=("${numbers[@]:2}")
echo "From index 2 onward: ${rest[@]}"
Login to try C/C++/Java/PHP code in the editor
Negative Offsets End से Count करते हैं
एक negative offset array के end से backward count करता है, लेकिन इसे minus sign से पहले एक space (या parentheses में wrapped) के साथ लिखा जाना चाहिए, जैसे ${arr[@]: -2}, otherwise Bash इसे एक slice के बजाय ${var:-default} expansion के रूप में parse करता है।
${items[@]:-2} (space के बिना) का मतलब पूरी तरह कुछ अलग है -- यह default-value expansion है, slice नहीं।उदाहरण: Negative Offsets Count From the End
#!/bin/bash
items=(one two three four five)
last_two=("${items[@]: -2}")
echo "Last two items: ${last_two[@]}"
Login to try C/C++/Java/PHP code in the editor
Strings को वैसे ही Slice करना
वही identical :offset:length syntax directly plain string variables पर भी काम करता है, array element position के बजाय character position से एक substring निकालते हुए।
यह consistency arrays और strings दोनों में slicing syntax को याद रखना आसान बनाती है।
उदाहरण: Slicing Strings the Same Way
#!/bin/bash
text="Hello, World!"
echo "Substring: ${text:7:5}"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि negative offsets एक normal index जैसे start से count करते हैं; एक negative offset का मतलब है 'end से count करो', और इसे minus sign से पहले एक space या parentheses चाहिए (
${arr[@]: -1}) ताकि Bash इसे:-default-value operator के साथ confuse न करे। - Length argument को end index मानने की गलती करना bजाय एक count के;
${arr[@]:2:3}index 2 से शुरू करके 3 elements लेता है, इसका मतलब 'index 3 तक' नहीं है। - यह भूल जाना कि slicing syntax दोनों arrays (
${arr[@]:offset:length}) और strings (${str:offset:length}) पर same rules के साथ लागू होती है, लेकिन वे respectively elements बनाम characters पर operate करती हैं।
- Syntax:
${arr[@]:offset:length}-- indexoffsetसे शुरू करें,lengthelements तक लें। lengthछोड़नाoffsetसे array के end तक सब कुछ लेता है।- एक negative offset array के end से count करता है, लेकिन
-से पहले एक space चाहिए (${arr[@]: -2}):-default-value expansion के रूप में parse होने से बचने के लिए। - वही
:offset:lengthslicing syntax plain strings पर काम करता है, array elements के बजाय characters पर operate करते हुए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: