|
From: atoms.h <tho...@gm...> - 2014-05-20 13:33:51
|
Hi all,
i have a strange problem regarding string variables and iterations. What I'd
like to do is to handle operations for several input files in a loop:
$ list = "file1.dat file2.dat file3.dat ..."
$ item(n) = word(list,n)
$ do for [i = 1 : words(list)]{
$ file = item(i)
$ total = `echo $(awk '/Total/ {print $4}' @file)`
$ }
This always complains that 'file' is not a string (for the awk command).
Outside the loop, however, it works:
$ list = "file1.dat file2.dat file3.dat ..."
$ item(n) = word(list,n)
$ do for [i = 1 : words(list)]{
$ file = item(i)
$ }
$ total = `echo $(awk '/Total/ {print $4}' @file)`
So there seems something strange is happening in the loop which I don't
quite understand. Like if the awk command would be executed first or
something...
If anyone has some inside or better idea I would very much appreciate it!
Kind regards,
Thomas
--
View this message in context: http://gnuplot.10905.n7.nabble.com/string-variables-and-iteration-tp18434.html
Sent from the Gnuplot - User mailing list archive at Nabble.com.
|
|
From: sfeam <sf...@us...> - 2014-05-20 15:40:29
|
On Tuesday, 20 May 2014 06:33:44 AM atoms.h wrote:
> Hi all,
>
> i have a strange problem regarding string variables and iterations. What I'd
> like to do is to handle operations for several input files in a loop:
>
> $ list = "file1.dat file2.dat file3.dat ..."
> $ item(n) = word(list,n)
> $ do for [i = 1 : words(list)]{
> $ file = item(i)
> $ total = `echo $(awk '/Total/ {print $4}' @file)`
> $ }
You cannot change the definition of a macro inside a do loop.
The macro is expanded before the loop is executed, so the
resulting expansion will be the same every time through the
loop.
But I don't see from the fragment there why you need to use
a macro anyhow. What is wrong with using item(i) directly?
Ethan
> This always complains that 'file' is not a string (for the awk command).
> Outside the loop, however, it works:
>
> $ list = "file1.dat file2.dat file3.dat ..."
> $ item(n) = word(list,n)
> $ do for [i = 1 : words(list)]{
> $ file = item(i)
> $ }
> $ total = `echo $(awk '/Total/ {print $4}' @file)`
>
> So there seems something strange is happening in the loop which I don't
> quite understand. Like if the awk command would be executed first or
> something...
> If anyone has some inside or better idea I would very much appreciate it!
>
> Kind regards,
> Thomas
|
|
From: atoms.h <tho...@gm...> - 2014-05-21 07:11:35
|
Thanks Ethan for your quick reply!
One problem is that I have to use the macro for
$ `echo $(awk '/Total/ {print $4}' @file)`
otherwise i cannot pass the variable to bash.
What also doesn't work (and I have no idea as to why that is) is to do
$ `echo $(awk '/Total/ {print $4}' @item(i))`
Then I get error because of the brackets....
Secondly, I realize that this probably wasn't the best example for what I
actually want to do.
Because 'total' gets overwritten for every file. My real goal would be
something like
$ total(i) = sprintf("%d",`echo $(awk '/Total/ {print $4}' @file)`)
So store the values in a fake-array....
Kind regards,
Thomas
--
View this message in context: http://gnuplot.10905.n7.nabble.com/string-variables-and-iteration-tp18434p18438.html
Sent from the Gnuplot - User mailing list archive at Nabble.com.
|
|
From: sfeam <sf...@us...> - 2014-05-21 14:56:45
|
On Wednesday, 21 May 2014 12:11:29 AM atoms.h wrote:
> Thanks Ethan for your quick reply!
>
> One problem is that I have to use the macro for
> $ `echo $(awk '/Total/ {print $4}' @file)`
> otherwise i cannot pass the variable to bash.
Break it down into steps:
awkcommand = "awk '/Total/ {print $4}' ".file
system( "echo $(" . awkcommand . ")" )
or something like that.
> What also doesn't work (and I have no idea as to why that is) is to do
> $ `echo $(awk '/Total/ {print $4}' @item(i))`
It seems you misunderstand what a macro is in gnuplot.
It is not some kind of function that is executed in-line.
Gnuplot macros do not take parameters.
They are basically a text-substitution mechanism that operates
before the command is executed. Think of it as running a
search-and-replace operation in a text editor before
feeding the result to gnuplot. The editor has no idea about
gnuplot syntax like do loops or if statements - it applies
the search-and-replace everywhere.
> Then I get error because of the brackets....
>
> Secondly, I realize that this probably wasn't the best example for what I
> actually want to do.
> Because 'total' gets overwritten for every file. My real goal would be
> something like
> $ total(i) = sprintf("%d",`echo $(awk '/Total/ {print $4}' @file)`)
> So store the values in a fake-array....
Sorry, gnuplot doesn't have arrays.
But it does have functions, so if your total() function can be
evaluated at the time it is used then your command above might
be close to working.
Ethan
|