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 12:37:13 +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; }
>

here is a substitude version , speed is in question , but i wouldnt learn
anything without writing such ..

cat u.1 ; printf \###\\n ; bash u.1
unset -v a f r R                                              a=( 0 1 2 3 )
f=2 r=a

 fidx() { declare -gn res=$3 ; declare f c ; declare -a r ; unset -v f c r
mapfile -d '' c < <( declare -p "$1" )
r=${c//=\"$2\"/$'\1'}
 [[ $r == $c ]] &&                                            return
r=${r%%$'\1'*} r=${r##*\[} res=${r%]}         }


fidx $r $f R
declare -p R
###                                                           declare --
R="2"


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?
>
> Cheers,
>
> Chris
>


reply via email to

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