Re: recursively calculate values



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


.



Relevant Pages

  • Re: recursively calculate values
    ... an explicit evaluation command for any deferred evaluation, ... use command substitution $to embed the program in your shell ... use eval and printf to display the results ...
    (comp.unix.shell)
  • Re: Cannot upgrade SQL Server 2005 Reporting Services from Standard to Enterprise
    ... You did not show us the command you used for command prompt installation, however did you try something like the following? ... Are you sure your SQL Server Instance isn't an Evaluation Edition? ... Server that is running SQL Server 2005 Reporting Services, ...
    (microsoft.public.sqlserver.setup)
  • Re: Purchase microcontroller dev. kit
    ... Even on compilers which don't support it directly, the way to deal with code like that is clearly explained in user manuals and FAQs. ... no one doing embedded development on a small microcontroller would write code like that anyway - using printf is a sure sign you don't know what you are doing. ... Having a command line interface is also ...
    (sci.electronics.design)
  • Re: workaround to make korn shell aliases that can take parameters
    ... It does require the apply command, ... printf>&2 '\n' ...
    (comp.unix.shell)
  • Re: Tcl_RecordAndEvalObj ... and history usage in C(++) ...
    ... 'DecrRefCount' when it adds the command to the history and then frees ... | evaluation with Tcl_DecrRefCount? ... Incr/DecrRefcount on the new string obj it creates. ... advantage in creating a tcl object only to contain a script only for ...
    (comp.lang.tcl)