Skip to main content

Questions tagged [associative-array]

Filter by
Sorted by
Tagged with
-2 votes
1 answer
60 views

Associative Array contains filenames and paths

I have a pipeline that deploys scripts into a temporary dir. I need to rellocate them into their actual proper directory. #!/usr/bin/sh DEPLOY_HOME=/opt/xxx function getDeployedFiles { ls ${...
KrOo Pine's user avatar
  • 107
0 votes
2 answers
80 views

Iterating arrays in TCL where array names have a numeric suffix

In TCL, I have a few arrays whose names have a numeric suffix (i.e., whose names end with a number), like below: array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ] array set ps1 [ list 0 ...
poppycock's user avatar
2 votes
1 answer
555 views

What is difference between these two declarations of associative arrays in Bash?

I am playing a bit with associative arrays in Bash and I found the following difference when declaring the exact same associative array with and without declare. The code is as follows: #!/usr/bin/env ...
chemacabeza's user avatar
1 vote
1 answer
52 views

What <COMMAND> will make `declare -A ASSOCIATIVEARRAY=( $( <COMMAND> ) )` work?

bash evaluates the following expression without any objections: declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue ) ...but it does not like this one one bit: declare -A SPANISH=( $( echo &...
kjo's user avatar
  • 16k
3 votes
1 answer
283 views

duplicated entries of an array in awk

I have a file with 4 columns. When I put these 4 columns into an array using NR as the index, the entries are duplicated somehow. See below for an elaboration of the issue. The first 5 lines of the ...
Xuan's user avatar
  • 45
1 vote
1 answer
128 views

index of an array is not recognized in awk

I have two tab separated files, each with two columns. I want to create a file which contains overlapping elements by column 1 of the two files. To do so, I put file 1 in an array first then scanned ...
Xuan's user avatar
  • 45
1 vote
3 answers
3k views

bash loop associative array with variable array name

I have a lot of associative arrays and i want to use only 1 loop. select the array for loop by a given name I want to select/build a part of the arrayname with a variable and than loop with that name, ...
Z0OM's user avatar
  • 4,491
1 vote
2 answers
1k views

How to count the number of lines per file in a directory, then create summary of number of files with n lines

I'm trying to create a summary of how many files in a directory have n number of lines in them. I'm using wc -l * | sort to print out the number of lines against the name of each file. What I'm trying ...
Doodling's user avatar
7 votes
2 answers
3k views

How to set environment variables dynamically for command?

I would like to use some kind of minimalistic template engine with pure bash and envsubst: user@host:~$ env -i FOO=foo BAR="bar baz" envsubst '$FOO,$BAR' \ <<< 'Hello "$FOO&...
user00705358365913's user avatar
0 votes
4 answers
438 views

Can't seem to access associative array with named reference in bash

# SETUP PHP81 SYMLINKS declare -A cgi=([path]="/opt/remi/php81/root/usr/bin/php-cgi" [filename]="php-cgi81") declare -A config=([path]="/opt/remi/php81/root/usr/bin/php-config&...
Corey Rosamond's user avatar
2 votes
3 answers
2k views

How can I create a JSON object from an associative array in shell using jo?

I know about How to create JSON from associative array but that's not my problem. I have this associative array: declare -A aliases aliases[Index]=components/Index/Exports aliases[Shared]=components/...
Saeed Neamati's user avatar
0 votes
1 answer
376 views

How does awk handle collisions in a hashmap?

Does awk use separate chaining, open addressing or does it have its own way of handling collisions in a hashmap? Do gawk and nawk implement the same algorithm? Thank you.
Zidani Mehdi's user avatar
3 votes
1 answer
120 views

How can i use a parameter as an inner array-name to my script?

When i write a script named array_call_self.sh as follows #!/bin/bash declare -A num word word=( [a]='index_a' [b]='index_b' [c]='index_c' ) num=( [a]=1 [b]=2 [c]=3 ) array=${$1[@]} for i in ${$...
AKA4's user avatar
  • 41
2 votes
1 answer
2k views

bash associative array where values are variables, print each value's variable name

I have the following associative array. var1="dog" var2="cat" var3="moose" declare -A asar01=( ["one"]="$var1" ["two"]="$var2" [&...
Dave's user avatar
  • 651
2 votes
2 answers
1k views

Bash pass an associative array to/from a background function

I am trying to pass a bash associative array by reference into a function and then be able to see the changed content back in the main script after the function is complete. I have found what seems to ...
demarcmj's user avatar
  • 231
0 votes
1 answer
102 views

Using variables defined in other bash scripts [duplicate]

I have a bash script where I define UTF-8 Greek Symbols in a file named greek-utfb.sh. I want to run tests that display the variables for printing the greek letters in the file greek-utfb-scout. --- ...
Isabel's user avatar
  • 89
0 votes
0 answers
498 views

Associative array in shell script

I am trying to map a hostname to a queue manager name using the following array. I get an error with bad array script. What am I doing wrong here declare -A managers while read -r mgr host; do ...
MO12's user avatar
  • 389
0 votes
1 answer
40 views

Replace last column of a piped result by the help of lookup array

I already have a shell command which creates lines like this one (from a long ss -an....|...|...): 5 10.1.1.20 3307 (Nb IP port) I have an echo command to simulate the answer: echo -e "5 10.1.1....
Christian COMMARMOND's user avatar
1 vote
1 answer
1k views

How to retrieve items from an array of arrays?

Hello StackExchange pros! I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it ...
jamesrg71's user avatar
5 votes
2 answers
1k views

Set a key with spaces in an associative array in Zsh

In Bash (4 or above), if I have an associative array dict, I can set its value like dict[apple count]=1 and I would be able to access it with ${dict[apple count]}. Does Zsh allow space in key names? ...
Franklin Yu's user avatar
  • 1,269
6 votes
2 answers
6k views

Bash: value too great for base when using a date as array key

I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until ...
Polizi8's user avatar
  • 285
19 votes
1 answer
2k views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
Stéphane Chazelas's user avatar
9 votes
1 answer
2k views

In ZSH, how do I unset an arbitrary associative array element?

An associative array I have has arbitrary keys, including keys containing backquotes, brackets, etc: $ typeset -A arr $ key='`' $ arr[$key]=backquote $ echo $arr[$key] backquote I now need to unset ...
Michaël's user avatar
  • 814
2 votes
1 answer
3k views

How to create JSON from associative array

I am writing a bash script on CentOS 7.5 that will execute some MongoDB commands. One of these commands will set replication servers. According to project, number of servers can be different. I have ...
BlackCrystal's user avatar
0 votes
0 answers
192 views

Unexpected behavior during index assignment in a Bash array [duplicate]

I'm having trouble assigning values to a specific bash index, but apparently only when the index variable is set using a while read loop. Taking this code as a test example: #!/bin/bash read -d '' ...
steinocaro's user avatar
4 votes
1 answer
470 views

Indirect parameter expansion in associative array

Following this answer, I want to apply the approach on my script. The basics of it is: foo="bar" baz="foo" echo "${!baz}" bar I want to provide translated strings, like ...
schrodingerscatcuriosity's user avatar
4 votes
4 answers
2k views

zsh testing existence of a key in an associative array via indirect expansion

So I know that you can test for the existence of a regular parameter via indirect expansion by doing something like: foo=1 bar=foo (( ${(P)+bar} )) && print "$bar exists" And I know you can ...
Matt Wilder's user avatar
7 votes
3 answers
8k views

Getting the key corresponding to a particular value in an associative array

I declare an associative array: declare -A array=([a]=blue [b]=red [c]=yellow) now I can do: echo ${array[@]} --> blue red yellow or echo ${array[b]} --> red or echo ${!array[@]} -->...
pietro letti's user avatar
1 vote
0 answers
2k views

How to make a simple menu in bash from an associative array

I am refactoring a script that displays a text menu similar to this: Select a mounted BTRFS device on your local machine to backup to. 1) 123456789abc-def012345-6789abcdef012 (/) 2) 123456789abc-...
MountainX's user avatar
  • 18.6k
1 vote
1 answer
429 views

How to declare -A MYASSOCIATIVEARRY globally? [duplicate]

I have the same exact problem as described in this SO post ("bash associative array key string with colon is giving error"): https://stackoverflow.com/q/40406187/10639803 The solution is to use ...
datsb's user avatar
  • 323
2 votes
2 answers
326 views

I am having difficulties with back-reference in awk

Recently, I am into security logs and want to make it better way on bash-shell. I found out in awk back-references are only stored by 9. But I need to use 10 back-references. Tried awk '{print ...
KeiTheNoop's user avatar
1 vote
1 answer
44 views

I am having difficulties during manipulating log file with awk and shellscript [closed]

I have a difficulty with awk. I want to concatenate columns if first fields and 3field matches from 2 different files with awk. Probably, It'd better to use array function, I am very confused with it. ...
KeiTheNoop's user avatar
2 votes
1 answer
6k views

how to combine 2 arrays into one associative array

i need to combine ARRAY1 and ARRAY2 into an associative array like ARRAY. i'm using this code: mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch) mapfile -t ARRAY2 < <(...
BlackCrystal's user avatar
0 votes
0 answers
161 views

bash: how to pass variables to a loop within a process substitution

How do you pass variable values within a process substitution to a loop within that process substitution? I'm reading a csv file into an associative array, and while reading each line, I would like ...
gj.'s user avatar
  • 11
0 votes
0 answers
18 views

associative array execution order problem [duplicate]

in a bash script i'm using an associative array to create tables, tablespaces, indexes and partitions in my database. this is the script (the main script is too long with complicated queries so i ...
BlackCrystal's user avatar
2 votes
1 answer
2k views

Global associative array only visible when the script is sourced a second time

I have the following script called .bash_functions.test which is already sourced by my .bash_functions script: # vim: set syn=sh noet: mp4Options_BIS="-movflags +frag_keyframe" declare -A ...
SebMa's user avatar
  • 2,341
0 votes
1 answer
2k views

updating one file based on values in another with AWK

I have two files file1.csv (20 columns 410k rows) and data.csv (4 columns 1800 rows). What I am trying to do is if data.csv 1st column matches file1.csv 2nd column overwrite 1st column in file1.csv ...
peti27's user avatar
  • 51
1 vote
1 answer
210 views

Assoc. Array not being redeclared?

I have a program that acts like a menu. It has an associative array called config such as: declare -A config=( [h]="?" [c]="?" [x]="?" [l]="?" [t]="?" [n]="?" ) In the main loop there's a check to ...
KuboMD's user avatar
  • 429
2 votes
2 answers
2k views

no matches found when using associative arrays in zsh

I am encountering no matches found when using map in zsh: #!/bin/zsh declare -A map=(["8761"]="Eureka服务" ["11001"]="用户微服务") Why would this happen, and how can I fix it? This is the error: ~/source/...
Dolphin's user avatar
  • 679
1 vote
2 answers
2k views

Merge duplicate keys in associative array BASH

I've got an array that contains duplicate items, e.g. THE_LIST=( "'item1' 'data1 data2'" "'item1' 'data2 data3'" "'item2' 'data4'" ) Based on the above, I want to create an associative array that ...
Bart's user avatar
  • 2,294
0 votes
1 answer
41 views

calling associative arrays [closed]

Team, am setting some variables in an associative array but its output is not resulting anything.. any hint?> #/bin/bash #IOEngine="psync" #TestType="read" IOEngine="libaio" TestType="randread" ...
fma abd's user avatar
2 votes
1 answer
495 views

Create persistent array in bash

I want an associative array in my bashrc file and I want to add to or delete from it whenever needed, but if I put the array declaration in the bashrc file it will get redeclared every time bash is ...
aderchox's user avatar
  • 731
10 votes
4 answers
2k views

Inverting an associative array

Let's say I have an associative array in bash, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values ...
Kusalananda's user avatar
  • 346k
10 votes
1 answer
5k views

Pass associative array as parameter list to script

In a script I have an associative array like: declare -A VARS=( ["key1"]="value1" ["key2"]="value" ) Is there a single command to transform that into a parameter list in the form --key1=value1 --...
Matteo Tassinari's user avatar
6 votes
3 answers
11k views

linux bash dictionary check if empty

How to check if a dictionary (associative array) is empty? I just declare one using declare -A dict. I want to know if it is just declared but not have any key.
focus zheng's user avatar
5 votes
4 answers
10k views

Making associative array based on another associative array

I made an associative array as follows. To give a few details, the keys refer to specific files because I will be using this array in the context of a larger script (where the directory containing the ...
mf94's user avatar
  • 219
7 votes
1 answer
11k views

Bash return an associative array from a function and then pass that associative array to other functions [closed]

I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working: #!/usr/bin/env bash function cwd { echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" &...
Thermatix's user avatar
  • 361
2 votes
1 answer
5k views

What does declaring a bash array with -A do?

In Example_1, when i have declared city to be an array with declare -A, why is Bangalore output first when the array is printed in the for loop? Bangalore Remote Kolkata Ahmedabad Hyderabad Pune ...
superman's user avatar
40 votes
6 answers
56k views

BASH associative array printing

Is there a way to print an entire array ([key]=value) without looping over all elements? Assume I have created an array with some elements: declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... ...
Dani_l's user avatar
  • 5,047
1 vote
0 answers
764 views

zsh parameter expansion flag (P) with associative arrays

In the zshexpn manual page (zsh version 5.1.1) we have: Parameter Expansion Flags (P): This forces the value of the parameter name to be interpreted as a further parameter name, whose value will be ...
Zorawar's user avatar
  • 845