0

is it possible to loop a simple array with key and value with a string in the [key] part ?

#!/bin/bash -x

wget url://stringlist1
wget url://stringlist2

#cat/sed some stuff ....

declare -A targetarray

loop $resultfromstringlist1
in strlistarray1 # filled with ( aka1 kaka hu3hu blabla )

loop $resultfromstringlist2
in strlistarray2 # filled with ( Thoralf Mirkoslav Pjotr Jan )

i=0;
for e in strlistarray1
do
   targetarray[$e]="${strlistarray2[$i]}"
   ((++i))
done

#wanted output 
echo ${targetarray[aka1]}  -> Thoralf

exit

i hope now is more clear

$e stays on $e its not filled with the string from strlist1 array

with manual setting works but not with loop bash version is 4.3

#!/bin/bash

unset aaaa
declare -A aaaa

str1="hans"
str2="klaus"

aaaa[$str1]="online";
aaaa[$str2]="offline";

echo ${aaaa[$str1]}
4
  • Your question is not very clear. Could you please give an example of what you want to see assigned to which array variables in this example you gave ? You gave constant string "USER" to be assigned to the array elements a[aka_one] a[aka_two] and a[aka_seven] I believe you wanted to do something else but can not figure out what.
    – MelBurslan
    Apr 20, 2016 at 17:28
  • @MelBurslan : yes i filled the loops only for explain what i try and show the debug i want to have in array [shortname]=LONGNAME so that i can search for both value and key not for index ... i updated the first post Apr 20, 2016 at 17:54
  • If you're asking what I think you're circuitously asking, yes, in bash 4.0 and later, you can use string indices for "associative" arrays; prior versions only have integer indices.
    – DopeGhoti
    Apr 20, 2016 at 19:13
  • @DopeGhoti manual works but not with loop i add a manual version to first post Apr 20, 2016 at 20:15

1 Answer 1

6

This demonstrates how to populate and query an associative array targetarray with keys and values from two simple arrays strlistarray1 and strlistarray2:

#!/bin/bash

declare -a strlistarray1=( aka1 kaka hu3hu blabla )
declare -a strlistarray2=( Thoralf Mirkoslav Pjotr Jan )

declare -A targetarray

i=0
for e in "${strlistarray1[@]}"; do
    targetarray["$e"]="${strlistarray2[i]}";
    ((i++))
done

for e in "${!targetarray[@]}"; do 
    echo $e --- ${targetarray[$e]}
done

To fetch the contents of a file (assuming one entry per line), try

IFS=$'\n'
declare -a strlistarray1=($(< yourfile))
unset IFS

To fetch data from a URL, try

IFS=$'\n'
declare -a strlistarray1=($(wget -qO - 'http://yoururl'))
unset IFS

(Setting IFS is required to avoid problems with fetched data containing spaces. If you're sure your input data doesn't have spaces, as in your question, the two IFS statements can be omitted.)

3
  • manual set no problem but not with dynamic fetched strings ....i canot declare staticly the values of strlistarray1 & strlistarray2 i fetch 2 files and generate the stringlists out of the files the extra declare does the thing ... very strange i fill my strings with 2x extra loops into the -a declared arrays so all will work ... Apr 20, 2016 at 21:00
  • @GorgoneImpertinence See updated answer for suggested use of wget to assign dynamic data. Maybe this helps.
    – Guido
    Apr 20, 2016 at 21:47
  • thanks ur solution for getting the online datas in the array looks nicer than my version Apr 21, 2016 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .