You are not logged in.
Pages: 1
Can somebody explain why the following works:
>#!/bin/zsh
>for i in {1..10}
>do
> set -A RESV$i `awk -v i=$i 'BEGIN {print "one two three"}' min_run_resV`
>done
But this doesn't
>#!/bin/zsh
>for i in {1..10}
>do
> RESV$i=(`awk -v i=$i 'BEGIN {print "one two three"}' min_run_resV`)
>done
I'm trying to learn zsh and think there must be some fundamental misunderstanding on my part going on here.
Last edited by aeroemike (2010-06-09 17:06:56)
Offline
The error is clearer if you don't try to assign an array to the variable:
$ RESV$i=foo
zsh: command not found: RESV1=foo
In other words, zsh doesn't recognise RESV$i as a name, instead treading the whole string as a command. I don't think there's a way to get around this except to use set.
Offline
How about this instead:
#!/bin/zsh
for i in {1..10}
do
set -A RESV$i $(awk -v i=$i 'BEGIN {print "one two three"}' min_run_resV)
done
You can replace `this` with $(this). $() can be nested.
Ogion
Last edited by Ogion (2010-06-12 21:03:02)
(my-dotfiles)
"People willing to trade their freedom for temporary security deserve neither and will lose both." - Benjamin Franklin
"Enlightenment is man's leaving his self-caused immaturity." - Immanuel Kant
Offline
Well, there is no reason not to use set -A, but I still find the error using foo$i=(blah) to be confusing.
Offline
Really, can you point my any language where this type of assignment work (foo$i=blah) without using a set(foo$i, blah) like syntax? Bash=no, python=no, c=no, perl=no, ruby=no, lua=no..... For me is natural that work this ways and give some crippled error.
Last edited by kazuo (2010-06-15 00:05:25)
Offline
Really, can you point my any language where this type of assignment work (foo$i=blah) without using a set(foo$i, blah) like syntax? Bash=no, python=no, c=no, perl=no, ruby=no, lua=no..... For me is natural that work this ways and give some crippled error.
Perl: ${"foo$i"} = 'blah'
They're known as "symbolic references" and they're naturally outlawed under "use strict 'refs'".
Offline
Ohh! thanks, dont know about this (Perl always come to invalidate my args... I so hate it ) I tried the naive $foo$i='blah'.
Last edited by kazuo (2010-06-15 00:47:34)
Offline
Really, can you point my any language where this type of assignment work (foo$i=blah) without using a set(foo$i, blah) like syntax? Bash=no, python=no, c=no, perl=no, ruby=no, lua=no..... For me is natural that work this ways and give some crippled error.
PHP:
$foo = 'bar';
$$foo = 'baz';
var_dump($bar);
prints:
string(3) "baz"
Offline
But can you do?
$i='1'
$foo$i='baz'
Offline
But can you do?
$i='1' $foo$i='baz'
Not quite, but this works:
$i = 1;
$bar = 'foo'. $i;
$$bar = 'baz';
var_dump($foo1);
Anyway, I'm REALLY not sure why anyone would ever do that, since $foo[$i] = 'baz'; works way better. In fact, why not do that for your original problem -- zsh supports multidimensional arrays.
Last edited by tavianator (2010-06-15 16:07:03)
Offline
kazuo wrote:But can you do?
$i='1' $foo$i='baz'
Not quite, but this works:
$i = 1; $bar = 'foo'. $i; $$bar = 'baz'; var_dump($foo1);
Anyway, I'm REALLY not sure why anyone would ever do that, since $foo[$i] = 'baz'; works way better. In fact, why not do that for your original problem -- zsh supports multidimensional arrays.
When you say "multidimensional arrays", I think of a variable with two pointers like foo[$i,$j]=[1,0;0,1] to give a 2x2 unity matrix. So $foo[1,1] gives 1, $foo[1,2] gives 0, etc. Is this possible? Disclaimer I think in Fortran.
Last edited by aeroemike (2010-06-15 18:05:43)
Offline
Pages: 1