๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #231

Re: Bash: check if a value is into an array

#231
From: Franco Martelli
Date: Wed, 21 Oct 2026 18:40
155 lines
9534 bytes
On 21/10/26 at 17:07, Greg Wooledge wrote:
> On Wed, Oct 21, 2026 at 16:45:30 +0200, Franco Martelli wrote:
>> # Populate playlist array with unique random values
>> while [ "$index" -le "$total" ]; do
>>          clip="$(/usr/bin/numrandom /1..$total/)"
> 
> I would try to avoid calling an external program inside this loop,
> especially if the purpose of the external program is just to choose
> a random number between 1 and n.  That can be done with a non-forking
> bash function call.

First of all thanks Greg for your patience explaining me coding. I'm 
interested about avoid to call an external tool inside the loop, do you 
have a snippet of code that provides random numbers in a specified range 
using a bash function?

> 
>>          if [[ ! ${playlist[@]} =~ "$clip" ]]; then      # [ -z
>> ${rndarr[clip]} ]; then
>>                                                          # rndarr[$clip]=1
>>                 playlist[$index]=$clip
>>                 ((index++))
>>          fi
>> done
> 
>> It works fine if I use the second array "rndarr[]". I use that array to
>> store 1 at the rndarr[$index] occurrence then, after incremented the index,
>> I check whether it's empty (no 1 stored) at the new $index value.
>>
>> My wishes is to avoid to use a second array (rndarr[]) and use playlist[]
>> array only to do this, but with the above code I have an endless loop, why?
> 
> OK, first note: using an associative array would be the typical way to
> store a set of "things that have already been chosen".  Your "rndarr"
> seems to be an indexed array, which I guess will also work as long
> as the "thing chosen" is a non-negative integer.
> 
> Trying to do this without a second array means you need to search through
> the entire first array every time to see whether the thing is already
> in the array.  That's an O(n) operation, while looking for an element
> in an associative array (or your second indexed array) should be a much
> faster O(1) operation.
> 
> Second note:  [[ ! ${x[@]} =~ "$i" ]]  is not even a safe way to check
> for the existence of an element "$i" in an array "x".  Consider the
> case where i = 5, and you've got 15 and 50 already in the array.  The
> expansion of ${x[@]} will include the text "15 50", and doing an =~ 5
> against that text will yield a false positive.

Yes I was afraid of this, but I wasn't sure that it happened.

> 
> Third note: many people, at this point, will cobble together some code
> like this:
> 
>      IFS=:
>      [[ ":${x[*]}:" =~ ":$i:" ]] ...
>      unset IFS
> 
> which compares the string :15:50: against the regex :5:.  This works,
> sort of.  But you're expanding the *entire* array as a string on
> every single loop iteration, generating a regular expression and
> applying it... it's overkill.  You can reduce the regular expression
> to a substring match:
> 
>      IFS=:
>      [[ ":${x[*]}:" = *":$i:"* ]] ...
>      unset IFS
>      
> which is slightly better, but you're still copying the entire array
> every single iteration.
> 
> Fourth note: if you want to check whether $i is in the array x[],
> the proper way is to *use a loop*:
> 
>      found=0
>      for element in "${x[@]}"; do
>          if [[ $element = "$i" ]]; then
>              found=1
>              break
>          fi
>      done
> 
> This avoids copying the entire array, and it allows you to stop early
> once you find the element.
> 
> Honestly, I would just use the associative array.  It'll be much more
> efficient than anything else you can do.

OK, I'll stay with my first version that uses a second array to mark a 
random value as assigned:

...
# Populate playlist array with unique random values
while [ "$index" -le "$total" ]; do
         clip="$(/usr/bin/numrandom /1..$total/)"
         if [ -z ${rndarr[clip]} ]; then
                 rndarr[$clip]=1
                 playlist[$index]=$clip
                 ((index++))
         fi
done
...

~$ time ./rndclips
1 120 93 30 13 104 113 28 18 101 12 131 15 107 109 58 36 75 76 57 64 45 
47 67 19 63 53 116 94 81 59 84 72 4 31 6 51 82 3 122 87 10 89 42 35 54 
90 96 99 38 34 95 16 136 25 5 21 123 80 133 79 66 60 132 39 8 11 118 49 
22 134 44 29 86 17 14 20 83 9 43 2 105 65 61 23 41 70 37 7 121 110 48 
125 97 73 74 27 108 68 78 103 69 50 126 106 119 62 1 88 135 52 114 92 85 
55 100 111 46 26 124 77 98 102 33 117 112 56 129 71 130 24 91 32 40 128 
115 127

real    0m5,228s
user    0m3,922s
sys     0m1,350s

This works, that was the time it takes to populate an array of 137 
elements. But it still suffers to fork a sub-process much more times of 
137 iterations.

> 
> Fifth note: you want to know why your loop never terminates, but I
> can't give you an answer to that.  I'd suggest turning on set -x to
> see what the script is doing.

I prefer to not investigate because traverse an entire array for each 
iteration it is not the right things to do (I changed my mind).

> 
>> I checked the remarkable ABS ยน handbook
> 
> The most remarkable thing about the ABS is how many flaws it contains.

:) but I learnt my Bash scripting reading ABS and I suppose I'm not 
alone, for instance at page #431 :
...
# Substring Replacement
# Replace first occurrence of substring with replacement.
echo ${arrayZ[@]/fiv/XYZ} # one two three four XYZe XYZe
#               ^         # Applied to all elements of the array.
...

This made me thinking that there was a standard way to query the array 
for a value.

You talked me about a Bash function able to return a random value 
between 1 and n, at page #107 of the ABS I see that I have to use the 
'modulo' operator to apply a range to a variable assigned with $RANDOM 
but it doesn't explain how to start from 1 and not from 0. Do I have to 
accept ranges from 0 to n and then discard the 0?

Kind regards,
-- 
Franco Martelli
Technical Details
Message-ID: <NQJod-71PP-13@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!diesel.cu.mi.it!bofh.it!news.nic.it!robomod
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!diesel.cu.mi.it!bofh.it!news.nic.it!robomod
From: Franco Martelli <martellif67@gmail.com>
Newsgroups: linux.debian.user
Subject: Re: Bash: check if a value is into an array
Date: Wed, 21 Oct 2026 20:40:01 +0200
Message-ID: <NQJod-71PP-13@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org  Wed Oct 21 18:30:41 2026
Old-Return-Path: <martellif67@gmail.com>
X-Amavis-Spam-Status: No, score=-1.849 tagged_above=-10000 required=5.3
 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
 DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001] autolearn=ham autolearn_force=no
X-Policyd-Weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 CL_IP_EQ_HELO_IP=-2 (check from: .gmail. - helo: .mail-wm1-x32c.google. - helo-domain: .google.)  FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -5.5
X-Gm-Message-State: AFq9FYKUcPls2wAycvaX/Ix8QjEQaunh6Z5JH3DltrIJP8etthNnkFn4
	EcErVoNwvCw30P6ZUIvVjTQy7evjI4cxZ25TjDmuSF+gD4y234SyyrbsV23F8A==
X-Gm-Gg: AYBFou08Mkcm92d+aelb51mPB1fJDnsprkI064IxICXPjHVonGkPu+a65cCL9H2yfVu
	IAt0yjMAppSpnvmWrrn9cECO43WT4Gt+3StGaCq/tWfVxh3rtxCf8XEwv2T8u0fgD+u6dS2c0SM
	RHa1zxrqHDK8Vr8Ag6R7xmPI8vzx4Y49UMU2hKLzJFhT1NfjF6kMHFMHgDxlwSMJGqbJ5p09oBJ
	IngtUf9Ah0IiFABUBPKxPcsOscRERBXoczzOEZ14boa5tZ/Ls/nmSYjn2Zrd3CpJ3faF7Yz1+yX
	fXkco7sqKDEbAdAlWydxDo2OpzQKRnUNW34Z6LkLS/VDz+eWvm+5yq8zVwlLZhU9t5O8g2KK2Na
	K7ibGRtL1TCQ6lNpatxtE2WaWARF3W7cY6YVM++wbrGrkpxm2nxGerw03DugBfQhouzg0IocW8V
	Kvqf479u78JmqjWnrV7HOZlikPpW+qHEBM6V7OBfKFIy5zNK4DJ/JKAdVRtHoxuK4irbC1m+x8d
	Mfk1qdOE4iopcvPDmgofh0NoJQnEweh0tXFqpGY2YDqKg5avZuxPzAJ3OClfIo5T0YVxdjnanBn
	sTqDdxZb+AHr
X-Received: by 2002:a05:600c:3492:b0:4a1:e4c4:f6a9 with SMTP id 5b1f17b1804b1-4a1fdd3d479mr63342135e9.23.1792607427890;
        Wed, 21 Oct 2026 11:30:27 -0700 (PDT)
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Content-Language: en-US
Autocrypt: addr=martellif67@gmail.com; keydata=
 xjMEXwx9ehYJKwYBBAHaRw8BAQdA8CkGKYFI/MK9U3RPhzE5H/ul7B6bHu/4BIhTf6LLO47N
 J0ZyYW5jbyBNYXJ0ZWxsaSA8bWFydGVsbGlmNjdAZ21haWwuY29tPsKWBBMWCAA+FiEET9sW
 9yyU4uM6QbloXEn0O0LcklAFAmhyroACGwMFCQ0ncuYFCwkIBwIGFQoJCAsCBBYCAwECHgEC
 F4AACgkQXEn0O0LcklAHVwD9H5JZ52g292FD8w0x6meDD8y/6KkNpzuaLHP6/Oo8kAIBAJsh
 aMB9LdCBJTMtnxU8JTHtAoGOZ/59UJWeZIkuWJUNzjgEXwx9ehIKKwYBBAGXVQEFAQEHQNP5
 V2q0H0oiJu89h1SSPgQDtkixXvUvRf1rNLLIcNpPAwEIB8J+BBgWCAAmFiEET9sW9yyU4uM6
 QbloXEn0O0LcklAFAmhyroACGwwFCQ0ncuYACgkQXEn0O0LcklASVwEAoEkHMEU7mHc0zmAu
 D2R1PYsDh9+3wQeied5PrF+HdakBAOeSGsf40GBew5umZuM59I04d1uXYAXGMP+jGN2RUtMA
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832538
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com
Approved: robomod@news.nic.it
Lines: 155
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Cc: debian-user@lists.debian.org
X-Original-Date: Wed, 21 Oct 2026 20:30:25 +0200
X-Original-Message-ID: <b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
 <20261021150743.GA21484@wooledge.org>