• create variables in a loop?

    From Dr Eberhard W Lisse@21:1/5 to All on Wed May 29 12:57:06 2024
    Hi,

    I would like to do something like

    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    Any idea on how to create (and fill variables through a loop)?

    greetings, el

    --
    To email me replace 'nospam' with 'el'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to Dr Eberhard W Lisse on Wed May 29 07:48:41 2024
    On Wed, 29 May 2024 06:57:06 -0400, Dr Eberhard W Lisse <nospam@lisse.na> wrote:
    Hi,
    I would like to do something like
    for i in USD GBP
    do
    $i=somevalue
    done
    does not work, of course.
    Any idea on how to create (and fill variables through a loop)?
    greetings, el

    There are many tutorials for getting started with shell scripting available, such as https://tldp.org/LDP/abs/html/index.html which has a "for/in loop" example at https://tldp.org/LDP/abs/html/loops1.html

    Which shell will you be working in?

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to nospam@lisse.NA on Wed May 29 12:09:31 2024
    In article <lbog03FfbrqU1@mid.individual.net>,
    Dr Eberhard W Lisse <nospam@lisse.NA> wrote:
    Hi,

    I would like to do something like

    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    The general answer is to use "eval", so something like:

    for i in USD GBP
    do
    eval $i=somevalue
    done

    But this doesn't scale well if "somevalue" is other than a simple string.
    (But see below...)

    Alternatively, with bash, you can use the "nameref" capability - see the
    man page for details.

    Update to add: this actually works:

    for i in one two three;do eval $i='"$(fortune)"';done

    --
    "Every time Mitt opens his mouth, a swing state gets its wings."

    (Should be on a bumper sticker)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Dr Eberhard W Lisse on Wed May 29 12:10:32 2024
    On Wed, 29 May 2024 12:57:06 +0200, Dr Eberhard W Lisse wrote:

    Hi,

    I would like to do something like

    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    Any idea on how to create (and fill variables through a loop)?

    echo $CAD $EUR
    for i in CAD EUR
    do
    eval "$i=someval"
    done
    echo $CAD $EUR

    The /eval/ operation is one of those "special built-in utilities
    that POSIX mandates all POSIX shells must support


    greetings, el





    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Elvidge@21:1/5 to Dr Eberhard W Lisse on Wed May 29 14:05:40 2024
    On 29/05/2024 at 11:57, Dr Eberhard W Lisse wrote:
    Hi,

    I would like to do something like

    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    Any idea on how to create (and fill variables through a loop)?

    greetings, el


    declare $i=somevalue
    possibly


    --
    Chris Elvidge, England
    RALPH WON'T "MORPH" IF YOU SQUEEZE HIM HARD ENOUGH

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Waitzmann@21:1/5 to All on Wed May 29 14:36:38 2024
    Dr Eberhard W Lisse <nospam@lisse.NA>:
    Hi,


    I would like to do something like


    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.


    Any idea on how to create (and fill variables through a loop)?


    Yes.


    for i in USD GBP
    do
    somevalue=... &&
    eval "$i"'="$somevalue"'
    done


    But note, that variable names consisting of only upper case
    letters, digits, and underscores are by convention reserved for
    the environment.  Therefore I recommend to do


    for i in USD GBP
    do
    somevalue=... &&
    eval 'currency_'"$i"'="$somevalue"'
    done


    instead, to get variables like currency_USD, currency_GBP, and so
    on.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Waitzmann@21:1/5 to All on Wed May 29 15:01:50 2024
    gazelle@shell.xmission.com (Kenny McCormack):
    Dr Eberhard W Lisse <nospam@lisse.NA> wrote:
    I would like to do something like


    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.


    The general answer is to use "eval", so something like:


    for i in USD GBP
    do
    eval $i=somevalue
    done

    But this doesn't scale well if "somevalue" is other than a
    simple string.


    First assign "somevalue" to a variable, not using "eval".  Then
    assign that variable's value to the variable named by "$i", using
    "eval"


    somevariablename=... &&

    # Note, that in the following assignment "..." may be any
    # valid shell expression expanding to a string, not just
    # a simple string:
    #
    somevalue=... &&

    # Note, how the apostrophes are placed around the right
    # hand part of the following assignment to be "eval"ed
    # but not around the name of the receiving variable to be
    # assigned to:
    #
    eval "$somevariablename"'="$somevalue"'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Dr Eberhard W Lisse on Wed May 29 18:25:11 2024
    On 29.05.2024 12:57, Dr Eberhard W Lisse wrote:
    Hi,

    I would like to do something like

    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    Any idea on how to create (and fill variables through a loop)?

    You've already got the technical answer to your (literal) question.
    Basically something like

    for currency in USD GBP EUR
    do
    eval "${currency}='some value $((++n))'"
    done
    printf "%s\n" "$USD" "$GBP" "$EUR"

    But I suggest to reconsider your software design approach!

    Mind that variables are for programmers, and that if you create
    variable names dynamically you either have to hard code the names
    in several places, or use 'eval' in several places; where ever you
    want to access them. (Which requires additional tests, to be sure.)

    Other approaches are (for example) defining data structures for
    the allowed variable name values (and attributes)

    currencies=( USD GBP EUR )
    factor=( 1.2 1.5 1.0 )

    value=4.3
    for (( i = 0; i < ${#currencies[@]} ; i++ ))
    do
    printf "%s: %g\n" "${currencies[i]}" $(( ${value} * ${factor[i]} ))
    done

    or use control constructs (to differentiate interrogated dynamic
    name values) like

    for f in data_file_*
    do
    curr=${f##data_file_}
    case ${curr} in
    (USD) val=1.2 ;;
    (GBP) val=1.5 ;;
    (EUR) val=1.0 ;;
    (*) exit 1 ;;
    esac
    done

    to be able to tell apart allowed values from undefined and errors.

    The question is; do I really want an *individual* _variable name_
    created? (Or is the technical [variable] name actually just data?)

    Janis


    greetings, el


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eberhard W Lisse@21:1/5 to Janis Papanagnou on Wed May 29 19:45:28 2024
    On 29/05/2024 18:25, Janis Papanagnou wrote:
    On 29.05.2024 12:57, Dr Eberhard W Lisse wrote:
    [...]
    I would like to do something like>>
    for i in USD GBP
    do
    $i=somevalue
    done

    does not work, of course.

    Any idea on how to create (and fill variables through a loop)?

    You've already got the technical answer to your (literal) question.
    Basically something like

    for currency in USD GBP EUR
    do
    eval "${currency}='some value $((++n))'"
    done
    printf "%s\n" "$USD" "$GBP" "$EUR"
    [...]

    Thanks to all having answered.

    greetings, el

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to nospam@lisse.NA on Wed May 29 18:31:38 2024
    In article <lbp7tpF24jbU1@mid.individual.net>,
    Eberhard W Lisse <nospam@lisse.NA> wrote:
    ...
    Thanks to all having answered.

    greetings, el

    Note that most responders have suggested "eval" (starting with yours
    truly), but I really think you should look into the "nameref" facility in
    bash. That's really the best solution - easier to use and more flexible
    than "eval".

    Here's an example:

    declare -n foo
    unset zzz one two three
    for foo in one two three;do foo=$((++zzz));done
    echo $one, $two, $three

    --
    Just for a change of pace, this sig is *not* an obscure reference to comp.lang.c...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Dr Eberhard W Lisse on Wed May 29 23:06:17 2024
    On Wed, 29 May 2024 12:57:06 +0200, Dr Eberhard W Lisse wrote:

    I would like to do something like

    for i in USD GBP do
    $i=somevalue
    done

    does not work, of course.

    for i in USD GBP; do
    declare -n a=$i
    a=somevalue
    done

    echo $USD $GBP

    output:

    somevalue somevalue

    This is in bash, of course.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Dr Eberhard W Lisse on Thu May 30 01:47:58 2024
    On Wed, 29 May 2024 12:57:06 +0200, Dr Eberhard W Lisse wrote:

    for i in USD GBP do
    $i=somevalue
    done

    Maybe you actually want a lookup table of, say, conversion factors in this case?

    declare -A factors
    for i in USD GBP; do
    factors[$i]=somevalue
    done

    echo "${!factors[@]}"
    echo "${factors[@]}"

    Output:

    GBP USD
    somevalue somevalue

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)