|
From: Ethan M. <eam...@gm...> - 2022-01-24 22:22:29
|
On Sunday, 23 January 2022 05:29:39 PST Marc Chantreux wrote:
> hello people,
>
> I'm considering using gnuplot to replace bc in some cases.
>
> There the for loop can be used for ploting and setting things.
> as the help says
>
> plot for [filename in "A.dat B.dat C.dat"] filename using 1:2 with lines
> plot for [basename in "A B C"] basename.".dat" using 1:2 with lines
> set for [i = 1:10] style line i lc rgb "blue"
> unset for [tag = 100:200] label tag
>
> however, it can't be used with print and the do loop requires {} even
> for 1 instruction. so when (what i think as) the "natural way" for
> a gnuplot beginner is
>
> f(x) = 2 * x + 3
> print for [ x = 0:20:2 ] x, f(x)
>
> i have to write
>
> f(x) = 2 * x + 3
> do for [ x = 0:20:2 ] {print x, f(x) }
>
> is there an internal/conceptual reason for print to not work like the
> others instructions?
Note that an iteration _inside_ a command is a different thing from
an iteration _outside_ a command. For example,
plot for [i=1:4] f(i,x) # Produces a single plot with 4 curves
do for [i=1:4] { plot f(i,x) } # Produces 4 plots each containing 1 curve
It would be possible to support iteration inside a print command
as in your first example above. But in order to be consistent with 'plot'
it would result in a single line of output, as contrasted with your second
example that produces 11 lines of output.
As to why the program doesn't already support this...
mostly because no one asked for it.
Generic iteration with syntax "for [<iteration spec>] { ... commands ... }"
is sufficient to handle most commands. There were two commands,
"plot" and "splot", where it seemed worthwhile to add a second form
of iteration specific to that command because it would allow a
distinctly different result as shown above. Adding it for "set"
was an afterthought; the benefit was only a small amount of convenience
in composing the command.
"print" is actually more like "plot" and "splot" than it is like "set".
So yeah, I think it's a reasonable suggestion and can be treated
as a Feature Request.
Ethan
|