bug-bash
[Top][All Lists]
Advanced

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

Re: declare -n next=arr[++elem] == nowork ? [[ -v


From: Alex fxmbsw7 Ratchev
Subject: Re: declare -n next=arr[++elem] == nowork ? [[ -v
Date: Mon, 8 Mar 2021 17:19:54 +0100

thank you for your answer
i have a question about [[ -v $next ]] i havent tried but you say it works
it would ? expand $next but wont it then check '[[ -v <content_of_next> ]]'
and fail ?
ill benchmark the usage of : $next ; before [[ -v now, if its minimal its
all fine i can use
i just remember it used to work

for the [[ -v arr[++elem] ]] style i do cause i can add in the loop to the
array and it continues over the new elements, till no more
also i think speed wise its very good, not so sure anymore lately

i also have another this-same-declare issue with a more complex data
structure
i have an assoc structure of two types in one array var
[$id] mainly, which is coming from the same assoc array by [$keyword] (
with separators, i just simplified here )
the actual code looks around like:

KWS[$kwns$_S_$[++kwstot]]=$code KWS[$kwns$_S_$kwstot$_S_]=$takes

and in a loop per keyword ( $kw ) :

 KWS[$kwns$_S_$_S_$kw]=$kwstot KWS[$kwns$_S_$kwstot$_S_$[++elem]]=$kw

$kwns is keyword_namespace, empty by default, i havent thought enough yet
technics to use it, however its a must

to eval the keywords:

while [[ -v kws[++i] ]] && kw=${kws[i]} t=$kwns$_S_ id=${KWS[$t$_S_$kw]}
code=${KWS[$t$id]} takes=${KWS[$t$id$_S_]} ; do
 ${code:+kwseval "$code" "${kws[@]:i:takes}" $[i+=takes]}
done

in fact i hope i send the three files as i am interested in an answer to:
'how can i declare -n right this issue'

each of the three files represents the content of the same-as-filename
named function
i made a linker to map aliases functions and assoc keywords from files to
.. bash .. ( id like to publish it but where, my bashhttpd isnt ready yet
but soon .. )

there are three different cases in the KWS assoc array
the ++id per +kw, which splits into two subelements, a 'code' key and a
'takes' key ( i didnt name em in the code anymore for only $_S_ $SUBSEP
$'\34' usage instead of long mixed text
and then theres the keyword in question entry that simply points to the id,
for the resolver to resolve to eval its code from the structure ..
i hope you didnt loose me too much yet

'how would i declare -n right the 3 (or 4) right ?'


On Mon, Mar 8, 2021 at 4:30 PM Chet Ramey <chet.ramey@case.edu> wrote:

> On 3/7/21 9:36 PM, Alex fxmbsw7 Ratchev wrote:
> > [msg(shbot)] # foo() { declare -a foo=( "$@" ) ; declare i=-1 ; declare
> -p
> > foo ; declare -n next=foo[++i]
> > now=foo[i] ; while [[ -v next ]] ; do : $now ; done ; printf -- $i ; } ;
> > foo '' 1 2 3
> > [shbot(~shbot@37.139.2.101)] declare -a foo=([0]="" [1]="1" [2]="2"
> [3]="3")
> > [shbot(~shbot@37.139.2.101)] -1
>
> This report would have been better sent to help-bash.
>
> It's a misunderstanding about how namerefs work. They're not like cpp
> macros or some kind of recursive variable expansion; they make one variable
> name stand for another.
>
> In your example, you have next='foo[++i]'. When you run [[ -v next ]], the
> nameref gets expanded and the shell looks for a variable named 'foo[++i]'.
> This naturally fails, and the loop terminates.
>
> If you want to iterate through the values of the array, you're better off
> ditching namerefs and using `eval' or [[ -v $next ]] (if you want to
> maintain the same code structure) or some other construct. It would be much
> clearer to simply use a for loop to iterate from 0 to ${#foo[@]}.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>

Attachment: +kw
Description: Binary data

Attachment: kwseval
Description: Binary data

Attachment: kws
Description: Binary data


reply via email to

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