Discussion:
create variables in a loop?
(too old to reply)
Dr Eberhard W Lisse
2024-05-29 10:57:06 UTC
Permalink
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'
David W. Hodgins
2024-05-29 11:48:41 UTC
Permalink
Post by Dr Eberhard W Lisse
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
Kenny McCormack
2024-05-29 12:09:31 UTC
Permalink
Post by Dr Eberhard W Lisse
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)
Helmut Waitzmann
2024-05-29 13:01:50 UTC
Permalink
Post by Dr Eberhard W Lisse
Post by Dr Eberhard W Lisse
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
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"'
Lew Pitcher
2024-05-29 12:10:32 UTC
Permalink
Post by Dr Eberhard W Lisse
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
Post by Dr Eberhard W Lisse
greetings, el
--
Lew Pitcher
"In Skills We Trust"
Helmut Waitzmann
2024-05-29 12:36:38 UTC
Permalink
Post by Dr Eberhard W Lisse
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.
Chris Elvidge
2024-05-29 13:05:40 UTC
Permalink
Post by Dr Eberhard W Lisse
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
Janis Papanagnou
2024-05-29 16:25:11 UTC
Permalink
Post by Dr Eberhard W Lisse
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
Post by Dr Eberhard W Lisse
greetings, el
Eberhard W Lisse
2024-05-29 17:45:28 UTC
Permalink
[...]
Post by Janis Papanagnou
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
Kenny McCormack
2024-05-29 18:31:38 UTC
Permalink
In article <***@mid.individual.net>,
Eberhard W Lisse <***@lisse.NA> wrote:
...
Post by Eberhard W Lisse
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...
Lawrence D'Oliveiro
2024-05-29 23:06:17 UTC
Permalink
Post by Dr Eberhard W Lisse
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.
Lawrence D'Oliveiro
2024-05-30 01:47:58 UTC
Permalink
Post by Dr Eberhard W Lisse
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

Loading...