help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of $@


From: alex xmb ratchev
Subject: Re: Use of $@
Date: Tue, 21 Feb 2023 11:39:02 +0100

On Tue, Feb 21, 2023, 11:28 AM Christof Warlich <cwarlich@gmx.de> wrote:

> Hi,
>
> just to improve my bash skills: The following functions prints the array
> index of a value if found:
>
> index() { local e="$1"; shift; local a=("$@"); for i in "${!a[@]}"; do
> [[ ${a[$i]} != $e ]] || { echo $i; break; }; done; }
>
> Thus, with e.g.: myarray=("a" "bc" "my value" "z")
>
> I get:
>
> $ index "my value" "${myarray[@]}"
> 2
>
> as expected. The only thing that bothers me is that I couldn't get away
> without the intermediate assignment of $@ to a new array (a): Is there
> really no way to avoid that, i.e. directly using $@ in the for-loop?
>

cat pidx ; printf \###\\n ; bash pidx                         unset -v arr
arr=( 0 1 2 3 )
                                                                pidx() {
declare -n n=$1 ; declare e=$2
 [[ -v n[e] ]] &&
printf '%s in %s .. yes\n' "$2" "$1"
  }

 pidx2() {
[[ -v $1[$2] ]]
 }

pidx arr 1 ; pidx arr 4
pidx2 arr 3 && echo ye ; pidx2 arr 5 && echo ye
###
1 in arr .. yes                                               ye

see it as checking , on common criterias
data looping , and copying , is cpu/hard

Cheers,
>
> Chris
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]