Re: recursively calculate values
- From: "Janis" <janis_papanagnou@xxxxxxxxxxx>
- Date: 30 Mar 2007 07:23:01 -0700
On 30 Mrz., 14:55, "mobidyc" <mobi...@xxxxxxx> wrote:
"Janis" <janis_papanag...@xxxxxxxxxxx> a écrit dans le message denews:1175254788.373848.266970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 30 Mrz., 12:43, "mobidyc" <mobi...@xxxxxxx> wrote:
Hello,
i know these data:
VAL_A=((60*2)+3)
VAL_B=(12+VAL_A)
VAL_C=1025
VAL_D=((6*VAL_B)/10+VAL_C)
how can i calculate the VAL_D value?
In shell...
VAL_A=$(( (60*2)+3 ))
VAL_B=$(( 12 + $VAL_A ))
VAL_C=1025
VAL_D=$(( (6 * $VAL_B)/10 + $VAL_C ))
printf "%d\n" $VAL_D
Janis
thanks Janis,
it seems so simple...but, if VAL_B appears before VAL_A, will do it
work?
In case you want an arbitrary order of the formulas you need either
"lazy evaluation", which is not directly supported by the shell, and
an explicit evaluation command for any deferred evaluation, or do
symbolic manipulation of the formulas.
Here is working code for the symbolic manipulation...
awk '
BEGIN { FS = "=" }
/^VAL_[A-Z]/ { form[$1] = $2 }
/EVAL/ { f = form[$2] ; print f
while (match (f, /VAL_[A-Z]/)) {
f = substr (f, 1, RSTART-1) \
form [substr (f, RSTART, RLENGTH)] \
substr (f, RSTART+RLENGTH)
print f
}
}
'
With the following data...
VAL_A=((60*2)+3)
VAL_B=(12+VAL_A)
VAL_C=1025
VAL_D=((6*VAL_B)/10+VAL_C)
EVAL=VAL_D
it will produce this output...
((6*VAL_B)/10+VAL_C)
((6*(12+VAL_A))/10+VAL_C)
((6*(12+((60*2)+3)))/10+VAL_C)
((6*(12+((60*2)+3)))/10+1025)
The rest will be your homework...
- remove the print statements and leave a single print _after_ the
while loop
- use command substitution $(...) to embed the program in your shell
context
- add literal arithmetic evaluation $((...)) around the substituted
command
- use eval and printf to display the results
Hope that helps.
Janis
and how can i add the $ and () necessary for doing it?
How about using an editor? (Or re-formulate your question.)
#> cat foo
VAL_B=(12+VAL_A)
VAL_A ((60*2)+3)
VAL_C=1025
VAL_D=((6*VAL_B)/10+VAL_C)
#> cat foo |while read value formula; do
if [ "$(echo $value |egrep -c "[[:alpha:]]")" = "1" ]; then
eval $value="\$\(${formula}\)"
else
eval $value="\$formula"
fi ; done
sh[3]: Syntax error at line 1 : `((' is not expected.
#> eval $VAL_A=\$\((60*2)+3)\)
sh: Syntax error: `(' is not expected.
#> eval $VAL_A=\$\(\$\(echo ((60*2)+3)\)\)
sh: Syntax error: `((' is not expected.
as you can see, i'm not a good scripting man ;(
Thanks for your input.
Regards,
Mobidyc
.
- Follow-Ups:
- Re: recursively calculate values
- From: mobidyc
- Re: recursively calculate values
- References:
- recursively calculate values
- From: mobidyc
- Re: recursively calculate values
- From: Janis
- Re: recursively calculate values
- From: mobidyc
- recursively calculate values
- Prev by Date: Re: recursively calculate values
- Next by Date: Re: recursively calculate values
- Previous by thread: Re: recursively calculate values
- Next by thread: Re: recursively calculate values
- Index(es):
Relevant Pages
|
|