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

Array Slicing का उपयोग

Slicing आपको एक array (या string) का सिर्फ एक portion निकालने देता है एक starting position और कितने elements लेने हैं यह specify करके, बिना एक manual loop लिखे।
Syntax
bash
${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

bash
#!/bin/bash
letters=(a b c d e f)
slice=("${letters[@]:1:3}")
echo "Slice: ${slice[@]}"

Length Omit करना

Length argument छोड़ देना, ${arr[@]:offset}, offset से लेकर array के end तक हर element लेता है। यह 'इस point के बाद सब कुछ' कहने का एक convenient तरीका है।

उदाहरण: Omitting the Length

bash
#!/bin/bash
numbers=(10 20 30 40 50)
rest=("${numbers[@]:2}")
echo "From index 2 onward: ${rest[@]}"

Negative Offsets End से Count करते हैं

एक negative offset array के end से backward count करता है, लेकिन इसे minus sign से पहले एक space (या parentheses में wrapped) के साथ लिखा जाना चाहिए, जैसे ${arr[@]: -2}, otherwise Bash इसे एक slice के बजाय ${var:-default} expansion के रूप में parse करता है।

Warning: ${items[@]:-2} (space के बिना) का मतलब पूरी तरह कुछ अलग है -- यह default-value expansion है, slice नहीं।

उदाहरण: Negative Offsets Count From the End

bash
#!/bin/bash
items=(one two three four five)
last_two=("${items[@]: -2}")
echo "Last two items: ${last_two[@]}"

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

bash
#!/bin/bash
text="Hello, World!"
echo "Substring: ${text:7:5}"
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. यह मान लेना कि negative offsets एक normal index जैसे start से count करते हैं; एक negative offset का मतलब है 'end से count करो', और इसे minus sign से पहले एक space या parentheses चाहिए (${arr[@]: -1}) ताकि Bash इसे :- default-value operator के साथ confuse न करे।
  2. Length argument को end index मानने की गलती करना bजाय एक count के; ${arr[@]:2:3} index 2 से शुरू करके 3 elements लेता है, इसका मतलब 'index 3 तक' नहीं है।
  3. यह भूल जाना कि slicing syntax दोनों arrays (${arr[@]:offset:length}) और strings (${str:offset:length}) पर same rules के साथ लागू होती है, लेकिन वे respectively elements बनाम characters पर operate करती हैं।
चैप्टर सारांश
  • Syntax: ${arr[@]:offset:length} -- index offset से शुरू करें, length elements तक लें।
  • length छोड़ना offset से array के end तक सब कुछ लेता है।
  • एक negative offset array के end से count करता है, लेकिन - से पहले एक space चाहिए (${arr[@]: -2}) :- default-value expansion के रूप में parse होने से बचने के लिए।
  • वही :offset:length slicing syntax plain strings पर काम करता है, array elements के बजाय characters पर operate करते हुए।

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.