You are not logged in.

#1 2010-06-09 16:41:42

aeroemike
Member
Registered: 2009-10-09
Posts: 21

zsh array assignment question

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

#2 2010-06-12 15:36:34

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: zsh array assignment question

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

#3 2010-06-12 21:02:14

Ogion
Member
From: Germany
Registered: 2007-12-11
Posts: 367

Re: zsh array assignment question

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

#4 2010-06-14 16:51:59

aeroemike
Member
Registered: 2009-10-09
Posts: 21

Re: zsh array assignment question

Well, there is no reason not to use set -A, but I still find the error using foo$i=(blah) to be confusing.

Offline

#5 2010-06-15 00:01:53

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: zsh array assignment question

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

#6 2010-06-15 00:35:39

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: zsh array assignment question

kazuo wrote:

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

#7 2010-06-15 00:45:17

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: zsh array assignment question

Ohh! thanks, dont know about this (Perl always come to invalidate my args... I so hate it tongue) I tried the naive $foo$i='blah'.

Last edited by kazuo (2010-06-15 00:47:34)

Offline

#8 2010-06-15 00:46:58

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: zsh array assignment question

kazuo wrote:

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

#9 2010-06-15 00:50:10

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: zsh array assignment question

But can you do?

$i='1'
$foo$i='baz'

Offline

#10 2010-06-15 16:06:31

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: zsh array assignment question

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.

Last edited by tavianator (2010-06-15 16:07:03)

Offline

#11 2010-06-15 18:05:26

aeroemike
Member
Registered: 2009-10-09
Posts: 21

Re: zsh array assignment question

tavianator wrote:
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

Board footer

Powered by FluxBB