Questions tagged [associative-array]
The associative-array tag has no summary.
64 questions
2
votes
1
answer
52
views
Why can't I have a single quote in a bash associative array index when testing with `test -v` [duplicate]
When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo "<<${X[$index]}>>"
if ...
-2
votes
1
answer
70
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 ${...
0
votes
2
answers
176
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 ...
2
votes
1
answer
564
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 ...
1
vote
1
answer
53
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 &...
3
votes
1
answer
309
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 ...
1
vote
1
answer
138
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 ...
1
vote
4
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, ...
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 ...
8
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&...
0
votes
4
answers
447
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&...
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/...
0
votes
1
answer
421
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.
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 ${$...
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"
[&...