Zsh array -a vs. -A question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Zsh array -a vs. -A question
# 1  
Old 01-26-2017
Zsh array -a vs. -A question

Inside a zsh function, I create a local array with
Code:
local -a arrayname

and a local associative array with
Code:
local -A arrayname

.

I also can create an array using set, like this:

Code:
set -A arrayname value1 value2 value3

In this form, I can not explicitly declare that an array is associative or non-associative (
Code:
set -a

exists, but has a completely different meaning).

Does someone know why this distinction was made?
# 2  
Old 01-26-2017
local is the same as typeset and it is a declaration. Set is an assignment to the array. To use associative arrays, typeset (declaration) must be done beforehand.. With regular arrays declaration is optional.
Code:
% set -A arr red 1 blue 2
% echo ${arr[red]}

% typeset -A arr
% set -A arr red 1 blue 2
% echo ${arr[red]}       
1
% echo ${arr[blue]}
2

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-26-2017
Comparison with bash4
Code:
$ typeset -A arr
$ arr=( [red]=1 [blue]=2 )
$ echo ${arr[red]}
1
$ echo ${arr[blue]}   
2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies

2. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. UNIX for Dummies Questions & Answers

Array question

I have attempted to create an array consisting of two items: #0 and #1. I am able to print the two items corrctly: arr=(hello "my name is") echo ${arr} hello echo ${arr} my name is However, when I try to run a for loop to print both objects: for i in ${arr } do echo $i done I get:... (2 Replies)
Discussion started by: locoroco
2 Replies

4. Programming

array question

Im new to C programming and am having trouble understanding the output of this code int array={4,5,8,9,8,1,0,1,9,3}; int *array_ptr; int main() { array_ptr=array; while((*array_ptr) != 0) array_ptr++;; printf("%d\n", array_ptr - array); return(0); } the output is 6 but I... (2 Replies)
Discussion started by: sacat
2 Replies

5. Shell Programming and Scripting

Help! Yet another check element in array Question

Greetings, DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned. I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want... (2 Replies)
Discussion started by: alan
2 Replies

6. Shell Programming and Scripting

Book and Links about Shells; and zsh question

HI, I would like to ask You about some good books or links where I can find information about shells, theoretical information. I will be grateful if You can help me And I have question about zsh loop trivial script: #!/bin/zsh for i in {1..100000} do echo $i; doneexec time is 10... (9 Replies)
Discussion started by: Physix
9 Replies

7. UNIX for Dummies Questions & Answers

question about zsh

hi, In bash, $ bind -P | grep yank-last yank-last-arg can be found on "\M-.", "\M-_". this allows me to press ALT key and the period (.) to yank the last argument of the previous command line into the current command line. How can I get the same behavior in zsh ? Thanks ... (0 Replies)
Discussion started by: Andrewkl
0 Replies

8. Shell Programming and Scripting

Array question

Hi all, I have a question does anyone know if it is possible to push or pop an array in the ksh environment? Could anyone give me a hint, because I am trying to merge 2 server files together and there are some names in the server is not proper anymore. Thank you in advance. (4 Replies)
Discussion started by: ahtat99
4 Replies

9. UNIX for Dummies Questions & Answers

Array question

When setting a variable, how would I go about making each result a new line? A very simple example would be: theFolders=`(ls -l /)` echo $theFolders This gives me all the folders as one variable and I need to be able to use each as its own variable. I'm sure I have to make this into an... (2 Replies)
Discussion started by: TheCrunge
2 Replies

10. Filesystems, Disks and Memory

Storage array question

We just purchased a MOD30 disk array strage system. We have 15 drives and 2 hot spares. We're running a database app with 8 data sets. I'm trying to get the best i/o speed out of my disk configuration. Right now I have 3 raid5 arrays setup. This seems to offer the same performance as having the... (1 Reply)
Discussion started by: ncmurf00
1 Replies
Login or Register to Ask a Question